home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / terms / kermit / b / ckvfio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-09  |  84.8 KB  |  2,777 lines

  1. #ifndef VMS
  2.       ERROR -- CKVFIO.C is used only on the OpenVMS(tm) Operating System
  3. #endif /* VMS */
  4.  
  5. #ifdef __ALPHA
  6. # define CKVFIO_OS_ARCH_STRING " OpenVMS(tm) AXP(tm)";
  7.          /* do nothing */
  8. #else
  9. # ifdef VAX
  10. #  module ckvfio "2.0-101"
  11. #  define CKVFIO_OS_ARCH_STRING " OpenVMS(tm) VAX(tm)";
  12. # else
  13. #  ifdef __GNUC__
  14. #     define CKVFIO_OS_ARCH_STRING " OpenVMS(tm) VAX(tm) (GCC)";
  15. #  else
  16. #     ERROR -- CKVTIO.C unknown architecture, neither VAX(tm) nor AXP(tm)
  17. #  endif /* __GNUC__ */
  18. # endif /* VAX */
  19. #endif /* __ALPHA */
  20.  
  21. char *ckzv = "File support, 2.0(101), 8 Aug 93";
  22. char *ckzsys = CKVFIO_OS_ARCH_STRING;
  23.  
  24. /* lt. 1992-10-08 End
  25.  */
  26.  
  27. /* C K V F I O  --  Kermit file system support for VAX/VMS.  */
  28.  
  29. /*
  30.   Author: Frank da Cruz (fdc@columbia.edu, FDCCU@CUVMA.BITNET),
  31.   Columbia University Academic Information Systems, New York City.
  32.  
  33.   Copyright (C) 1985, 1993, Trustees of Columbia University in the City of New
  34.   York.  The C-Kermit software may not be, in whole or in part, licensed or
  35.   sold for profit as a software product itself, nor may it be included in or
  36.   distributed with commercial products or otherwise distributed by commercial
  37.   concerns to their clients or customers without written permission of the
  38.   Office of Kermit Development and Distribution, Columbia University.  This
  39.   copyright notice must not be removed, altered, or obscured.
  40. */
  41.  
  42. /*
  43.   Originally adapted to VMS by:
  44.   Stew Rubenstein, Harvard University Chemical Labs, 1985,
  45.   Contributors:
  46.   Frank da Cruz (fdc), Columbia University Center, New York, NY (1985-93)
  47.   Stew Rubenstein, Harvard University Chemical Labs, Cambridge, MA (1985)
  48.   Martin Minow (MM), Digital Equipment Corporation, Maynard MA (1985)
  49.   Dan Schullman (DS), Digital Equipment Corporation, Maynard MA (1985)
  50.   Mark Buda (MAB), Digital Equipment Corporation, Nashua, NH (1989-90)
  51.   Terry Kennedy (TMK), St. Peter's College, Jersey City, NJ (1990-92)
  52.   William Bader (WB), Lehigh University, Bethlehem, PA (1990-92)
  53.   Gary Mussar (GM), Bell-Northern Research, Ottawa, Canada (1991)
  54.   James Sturdevant (JS) (1992)
  55. */
  56. /* Edit history
  57.  * 003 20-Mar-85 MM  fixed fprintf bug in zsout.c
  58.  * 004 21-Mar-84 MM  create text files in variable-stream.
  59.  * 005  8-May-85 MM  filled in zkself (not tested), fixed other minor bugs
  60.  * 006  5-Jul-85 DS  handle version number in zltor, zrtol
  61.  * 007 11-Jul-85 fdc fix zclose() to give return codes
  62.  * 008 19-Mar-86 fdc Fix system() for "!", zopeni() for REMOTE commands.
  63.  * 008 17-Sep-87 fdc Define PWDCMD.
  64.  * 009 (???)
  65.  * 010 24-Jan-88 fdc Add zgtdir() function, even tho it doesn't work...
  66.  * 011 14-Feb-89 mab Make zgtdir() work in V2/V3 C envirements,
  67.  *             Make zkself work using delprc() using Will Wood's changes.
  68.  * 012 26-Feb-89 mab Add function that searches for kermit.ini file in various
  69.  *                   ways
  70.  * 013 05-Mar-89 mab Add Barry Archers enhancements/fixes.
  71.  * 014 15-Mar-89 mab Check for non-null data, not array of pointers in
  72.  *                   zkermini
  73.  * 015 04-Apr-89 mab Add latent support for attribute packet.  Clean up
  74.  *             file name translation code.
  75.  * 016 05-Apr-89 mab Add PWP code to optimize packetizing.
  76.  * 017 16-Apr-89 mab PWP changes broke REMOTE command.  Fixed.
  77.  * 018 18-Apr-89 mab #ifdef chkfn.  This removes a lot of overhead.
  78.  *             Add code to gtdir() for V4.x.
  79.  * 019 12-Jun-89 mab Add PWP's encode logic
  80.  * 020 09-Jul-89 mab Add logic to check for system() availability
  81.  * 021 10-Jul-89 mab Fix SHOW USER USERNAME.  Added space after 'SHOW USER'.
  82.  * 022 27-Sep-89 mab Added zmail/zprint, plus added changes from CKUFIO.C
  83.  * 023 01-Dec-89 mab Add RMS file support
  84.  * 024 20-Jul-90 wb  Add support for old VAX C & VMS versions + zstrip & rename
  85.  * 025 29-Jul-90 tmk Change space command to show avail, not used (match spec)
  86.  * 026 29-Jul-90 tmk Hack out the RMS stuff - it can come back when it works
  87.  * 027 29-Jul-90 tmk Likewise the VMS V3 stuff - ancient history
  88.  * 028 29-Jul-90 tmk Replace the attribute stuff. It now works.
  89.  * 029 31-Jul-90 tmk Fix CWD command (via hack)
  90.  * 030 31-Jul-90 tmk Fix assorted bugs preventing remote commands from working
  91.  * 031 31-Jul-90 tmk Correctly handle interrupted remote commands
  92.  * 032 04-Aug-90 tmk Start work on full RMS support for input files
  93.  * 033 04-Aug-90 tmk Tack LF on end of subprocess output lines
  94.  * 034 04-Aug-90 tmk Complete work on full RMS support for input files
  95.  * 035 04-Aug-90 tmk Add support for Fortran CC, fill in recfm data
  96.  * 036 05-Aug-90 tmk Add trailing CRLF on print format files
  97.  * 037 12-Aug-90 tmk Start work on full RMS support for output files
  98.  * 038 12-Aug-90 tmk Honor first free byte (FFB) on SENDs
  99.  * 039 13-Aug-90 tmk Finished first cut of full RMS support for output files
  100.  * 040 29-Sep-90 tmk Add iswild() from FDC for edit 157
  101.  * 041 06-Oct-90 tmk Add filetype IMAGE support for outbound transfers. Note
  102.  *             that this doesn't currently work as the receiver overrides
  103.  *             it (must talk to fdc).
  104.  * 042 06-Oct-90 tmk Make logfiles MRS=80. Being able to edit them outweighs
  105.  *             any use for un-split lines.
  106.  * 043 17-Oct-90 wb  Make zclosf() remove delete mailboxes & deassign channels
  107.  *                   used to talk to the subprocess, so quotas are not used
  108.  *                   up after repeated mailbox use (installed by fdc).
  109.  * 044 19-Oct-90 fdc Changed zxcmd() to use the fp[] arrays in the normal way,
  110.  *                   and zsyscmd to call zxcmd(ZIFILE) rather than
  111.  *                   zxcmd(ZSYSFN).  Got rid of all calls to system(), used
  112.  *                   zsyscmd() instead, so commands like DIR could be
  113.  *                   interrupted.  Made zoutdump() return(-1) rather than
  114.  *                   exit() when "line too long for buffer", and increased
  115.  *                   line output buffer from 1K to 4K.
  116.  * 045 01-Nov-90 tmk Corrected behavior of error check on $create call so a
  117.  *             file supersede would work properly.
  118.  * 046 01-Nov-90 tmk Clone binary flag to ofile_bmode so we have a consistent
  119.  *             view of this flag during file operations - the binary flag
  120.  *             tends to toggle when we don't want/expect it to.
  121.  * 047 01-Nov-90 tmk Make IMAGE mode work. Note that image mode is only used
  122.  *             when VMS is sending a file, and includes all record
  123.  *             control characters not normally sent. Only useful in
  124.  *             unusual circumstances.
  125.  * 048 01-Nov-90 tmk Remove spurious \n from zsoutl() which caused debug logs
  126.  *             to have spurious <CR>'s when viewed with editors.
  127.  * 049 02-Nov-90 fdc Adapt to dynamic allocation of file i/o buffers.  Changes
  128.  *                   are within #ifdef DYNAMIC..#else..#endif brackets.
  129.  * 050 02-Nov-90 fdc Make zsyscmd() close inferior process.
  130.  * 051 ??-???-?? ??? Add ckermit_init logical, return 0 on wildcard operations.
  131.  * 052 24-Dec-90 tmk Fix performance problems after 32Kb w/ ASCII receives, fix
  132.              2-nulls-per-32Kb in binary mode bug (actually in ckcker.h,
  133.              this is a placeholder).
  134.  * 053 13-Jan-91 tmk Add support for SET FILE RECORD-LENGTH.
  135.  * 054 14-Jan-91 tmk Fix cases of /x/CR/LF/y/ and /x/CR/LF/y/CR/LF/ in ASCII
  136.  *             file receives.
  137.  * 055 16-Jan-91 tmk Log requested file type to debug log when receiving.
  138.  * 056 16-Jan-91 tmk Add support for all zstime() functions.
  139.  * 057 17-Jan-91 tmk Add support for zchkspa() function.
  140.  * 058 17-Jan-91 tmk Move debug() call into if clause in zxpand, per fdc.
  141.  * 059 18-Jan-91 tmk Support remote (DECnet) file accesses.
  142.  * 060 18-Jan-91 tmk Fix READ command.
  143.  * 061 30-Jan-91 tmk Support creation of UNDEFINED file types for brain-dead
  144.  *             BASIC implementation.
  145.  * 062 30-Jan-91 tmk Fix REMOTE commands when VERIFY is set.
  146.  * 063 29-Mar-91 tmk Add padding factor for received text files to accomodate
  147.  *             space taken up by record delimiters (per fdc).
  148.  * 064 29-Mar-91 gm  Remove unnecessary mem-mem moves during ASCII receives.
  149.  *             (Installed by tmk. To back out, #define OLD_WAY).
  150.  * 065 30-Mar-91 tmk First pass at implementing LABELED. Send only, dummy
  151.  *             data records.
  152.  * 066 02-Apr-91 tmk Finish first pass at LABELED. Send VMS filename, attri-
  153.  *             butes. Still need ACL's, "hidden" char. longword, recep-
  154.  *             tion.
  155.  * 067 09-Apr-91 tmk LABELED bugfixes - VMSFILE is 70 bytes, not 74, use the
  156.  *             xab$w_lrl field instead of rab$w_rsz, fab$w_deq instead
  157.  *             of xab$w_rsz, fab$b_bks instead of xab$b_bkz, always pro-
  158.  *             cess an even multiple of 512 bytes when LABELED.
  159.  * 068 14-Apr-91 tmk Don't use C definition of fab$b_journal as it doesn't ex-
  160.  *             ist before C V3.1. Compute it ourselves instead.
  161.  * 069 15-Apr-91 tmk Initial work on retrieving ACL information for LABELED.
  162.  * 070 16-Apr-91 tmk Make edits 066-069 compatible with DECnet.
  163.  * 071 21-May-91 tmk Address R. Weiner QAR item 2 (filesize).
  164.  * 072 21-Jun-91 tmk Check (and prohibit) spawns from captive accounts.
  165.  * 073 21-Jun-91 tmk Fix session logging (for Charlie Luce/DECUServe).
  166.  * 074 21-Jun-91 tmk Rework 071 to only apply to SPAWN/PUSH and not to the
  167.  *             pseudo-builtins like DEL, SPACE, WHO, PWD, etc.
  168.  * 075 21-Jun-91 tmk Fix possible endless loop when flushing output file in
  169.  *             zclosf() after zoutdump() error.
  170.  * 076 21-Jun-91 tmk First pass on handling inbound LABELED files.
  171.  * 077 14-Nov-91 tmk Fix zprint(), zmail() (need to use system() for these).
  172.  *                   This is a partial backout of 044.
  173.  * 078 14-Nov-91 tmk Various cleanups.  Delete files after successful mailing
  174.  *             or printing, remove dead code inside #ifdef COMMENT and
  175.  *                   #ifdef OLD_WAY, fix typo in spawning message, make sure
  176.  *                   all source lines < 80 chars.
  177.  * 079 22-Nov-91 fdc Change zmail(), zprint() error return values to improve
  178.  *             error reporting.
  179.  * 080 18-Jan-92 tmk Fix REMOTE so output from a remote command correctly dis-
  180.  *             plays on terminal. This has been broken since 040 or so.
  181.  * 081 10-Jun-92 tmk Add William Bader's fix for fixed-length files which have
  182.  *                   record attributes.
  183.  * 082 03-Jul-92 tmk Fix really bad bug introduced in 081 (which made *all*
  184.  *                   fixed-format files be sent as text).
  185.  * 083 15-Jul-92 jah Fix fencepost error in zoutdump when line breaks at 32K.
  186.  * 084 03-Aug-92 fdc Remove current directory from init file search.
  187.  * 085 26-Aug-92 tmk Add Bernd Onasch's fix for fgen().
  188.  * 086 28-Aug-92 tmk Fix bug reported by Bill Hoelzer where C-K would execute
  189.  *             a file named "." as a C-K initialization file.
  190.  * 087 04-Sep-92 tmk Fix bug reported by Chuck McMichael where C-K would not
  191.  *             set the FFB properly when receiving a labeled file which
  192.  *             did not have the FFB on a record boundary.
  193.  * 088 09-Sep-92 tmk Fix Hunter Goatley's problem with SPAWN command ignoring
  194.  *             Ctrl-C.
  195.  * 089 11-Sep-92  js Fixed malloc() in zmail().
  196.  * 090 28-Oct-92 tmk Fix null-byte error introduced by 087. Gee, this looked
  197.  *             so simple when I designed it.
  198.  * 091 02-Nov-92 tmk Start work on fixing spawn/push/remote commands, due to
  199.  *             popular whining.
  200.  * 092 03-Nov-92 tmk Finish up initial 091 work. Vote for Kermit!
  201.  * 093 03-Nov-92 fdc Change zkermini() to work with "-y" command-line option.
  202.  * 094 04-Nov-92 tmk Make zxpand() not return all files if given null string.
  203.  * 095 05-Nov-92 fdc Make zxcmd(), zclose(), etc, handle ZRFILE (OPEN !READ).
  204.  * 096 17-Feb-93 fdc prevent zopeno from calling zstime if date struct is NULL,
  205.  *                   and add support for ZMFILE (misc output file).
  206.  * 097 08-Apr-93 tmk Correctly handle "international VMS" which uses <> instead
  207.  *             of [] for directory delimiters.
  208.  * 098 16-May-93 fdc ANSIfication for GNU CC, from James Sturdevant, plus
  209.  *                   add FAB$M_PRN to list of text-file types, for VMS batch
  210.  *                   logs.
  211.  * 099 07-Jun-93 fdc Fix calculation of file size in zchki(), fix declaration
  212.  *                   of mbxnam[] (add one to size) to prevent overflow, which
  213.  *                   would result in failure of server to respond to REMOTE
  214.  *                   directory, etc.  Both fixes from Bill Glass.
  215.  * 100 21-Jun-93 fdc file_date[] and attr_date[] declarations in zstime()
  216.  *                   changed from long to unsigned long to prevent signed date
  217.  *                   comparisons, which could prevent SET FILE COLLISION
  218.  *                   UPDATE from working.  From James Sturdevant.
  219.  * 101  8-Aug-93 fdc Add types to all function declarations.
  220.  */
  221.  
  222. /* Definitions of some VMS system commands */
  223.  
  224. char *DIRCMD = "directory ";        /* For directory listing */
  225. char *DIRCM2 = "directory ";        /* For directory listing, no args */
  226. char *DELCMD = "delete ";        /* For file deletion */
  227. char *TYPCMD = "type ";            /* For typing a file */
  228. char *SPACMD = "show quota ";         /* Space/quota of current directory */
  229. char *SPACM2 = "show quota ";         /* Space/quota of specified dir */
  230. char *WHOCMD = "show users ";        /* For seeing who's logged in */
  231. char *PWDCMD = "show default ";        /* For seeing current directory */
  232.  
  233. /*
  234.   Functions (n is one of the predefined file numbers from ckermi.h):
  235.  
  236.    zopeni(n,name)   -- Opens an existing file for input.
  237.    zopeno(n,name)   -- Opens a new file for output.
  238.    zclose(n)        -- Closes a file.
  239.    zchin(n)         -- Gets the next character from an input file.
  240.    zsout(n,s)       -- Write a null-terminated string to output file, buffered.
  241.    zsoutl(n,s)      -- Like zsout, but appends a line terminator.
  242.    zsoutx(n,s,x)    -- Write x characters to output file, unbuffered.
  243.    zchout(n,c)      -- Add a character to an output file, unbuffered.
  244.    zchki(name)      -- Check if named file exists and is readable, return size.
  245.    zchko(name)      -- Check if named file can be created.
  246.    zchkspa(name,n)  -- Check if n bytes available to create new file, name.
  247.    znewn(name,s)    -- Make a new unique file name based on the given name.
  248.    zdelet(name)     -- Delete the named file.
  249.    zxpand(string)   -- Expands the given wildcard string into a list of files.
  250.    znext(string)    -- Returns the next file from the list in "string".
  251.    zxcmd(n,cmd)     -- Execute the command in a lower fork on file number n.
  252.    zclosf()         -- Close input file associated with zxcmd()'s lower fork.
  253.    zrtol(n1,n2)     -- Convert remote filename into local form.
  254.    zltor(n1,n2)     -- Convert local filename into remote form.
  255.    zchdir(dirnam)   -- Change working directory.
  256.    zhome()          -- Return pointer to home directory name string.
  257.    zkself()         -- Log self out
  258.    zsattr(struc zattr *) -- Return attributes for file which is being sent.
  259.    zkermini(n1,n2)  -- Find kermit.ini using default scanning process
  260.  */
  261.  
  262. /* Includes */
  263.  
  264. #include "ckcdeb.h"
  265. #include "ckcasc.h"
  266. #include "ckcker.h"
  267. #include "ckvvms.h"
  268. #include <stdio.h>
  269. #include <stat.h>
  270. #include <ctype.h>
  271. #include <rms.h>
  272. #include <ssdef.h>
  273. #include <descrip.h>
  274. #include <dvidef.h>
  275. #include <dcdef.h>
  276. #include <iodef.h>
  277. #include <jpidef.h>
  278. #include <signal.h>
  279. #include <string.h>
  280. #include <syidef.h>
  281. #include <uaidef.h>
  282.  
  283. #define MAXWLD 500            /* Maximum wildcard filenames */
  284.  
  285. /* external def. of things used in buffered file input and output */
  286.  
  287. #ifdef DYNAMIC
  288. extern CHAR *zinbuffer, *zoutbuffer;
  289. #else
  290. extern CHAR zinbuffer[], zoutbuffer[];
  291. #endif /* DYNAMIC */
  292.  
  293. extern CHAR *zinptr, *zoutptr;
  294. extern int zincnt, zoutcnt;
  295. extern int binary;
  296. extern int frecl;
  297. extern int rcflag;
  298.  
  299. extern long vernum;
  300.  
  301. /* Declarations */
  302.  
  303. FILE *fp[ZNFILS] = {             /* File pointers */
  304.     NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
  305. };
  306.  
  307. static long iflen = -1;            /* Input file length */
  308. static long oflen = -1;            /* Output file length */
  309. static int fcount;            /* Number of files in wild group */
  310. static char nambuf[255];        /* maximum size of a file spec */
  311. static char cwdbuf[NAM$C_MAXRSS];
  312. static struct iosb_struct tmpiosb;    /* For QIOW */
  313.  
  314. extern unsigned long vms_status;    /* Used by CHECK_ERR */
  315.  
  316. static int cflag;            /* Flag indicating console in use */
  317.  
  318. char *getenv(), *strcpy();        /* For finding home directory */
  319.  
  320. /* static */                /* Not static any more! */
  321. char *mtchs[MAXWLD],            /* Matches found for filename */
  322.   **mtchptr;                /* Pointer to current match */
  323.  
  324. static unsigned short mbx_chan;        /* Mailbox chan for REMOTE commands */
  325. static int subprocess_input = 0, sub_count;
  326. static char *sub_ptr, sub_buf[SUB_BUF_SIZE];
  327.  
  328. #define    SUPERSAFE            /* For safe subprocesses */
  329. static unsigned long sub_pid;
  330.  
  331. /*
  332.  * Structures for input (SEND) file
  333.  */
  334.  
  335. static    struct FAB fab_ifile;
  336. static    struct RAB rab_ifile;
  337. static    struct XABDAT xabdat_ifile;
  338. static    struct XABFHC xabfhc_ifile;
  339. static    struct XABPRO xabpro_ifile;
  340. static    struct XABALL xaball_ifile;
  341. static    int ifile_bmode;
  342. static    int ifile_bcount;
  343. static    char aclbuf[512];
  344. static    unsigned long uchar = 0;
  345.  
  346. /*
  347.  * Structures for output (RECEIVE) file
  348.  */
  349.  
  350. static    struct FAB fab_ofile;
  351. static    struct RAB rab_ofile;
  352. static    struct XABDAT xabdat_ofile;
  353. static    struct XABFHC xabfhc_ofile;
  354. static    struct XABPRO xabpro_ofile;
  355. static    struct XABALL xaball_ofile;
  356. static    struct XABRDT xabrdt_ofile;
  357. static    int ofile_dump;
  358. static    int ofile_bmode;
  359. static    int ofile_lblopts;
  360. static    int ofile_lblproc;
  361. static    char revdat[8];
  362. static    unsigned short revnum;
  363. static    char ofile_vmsname[255];
  364. static    char ofile_vmsacl[512];
  365. static    int ofile_acllen;
  366. static    short ofile_ffb;
  367.  
  368. /*
  369.  * Common RMS items
  370.  */
  371. static    unsigned long int rms_sts;
  372.  
  373. /*  I S W I L D  --  Tells whether filespec "str" is wild  */
  374. /*  Returns 0 if not wild, 1 if wild */
  375.  
  376. int
  377. iswild(str) char *str; {
  378.     char c;
  379.     while ((c = *str++) != '\0')
  380.       if (c == '*' || c == '%') return(1);
  381.     return(0);
  382. }
  383.  
  384. /*  Z K S E L F  --  Log self out.  */
  385.  
  386. VOID
  387. zkself() {
  388.     unsigned long int rms_s;
  389.  
  390.     rms_s = sys$delprc(0,0);
  391.     exit(rms_s == SS$_NORMAL);
  392. }
  393.  
  394. /*  Z O P E N I  --  Open an existing file for input. */
  395.  
  396. int
  397. zopeni(n,name) int n; char *name; {
  398.     debug(F111," zopeni",name,n);
  399.     debug(F101,"  fp","",(int) fp[n]);
  400.     if (chkfn(n)) return(0);
  401.     if (n == ZSYSFN) {            /* Input from a system function? */
  402.     debug(F110," zopeni called with ZSYSFN, failing!",name,0);
  403.     *nambuf = '\0';            /* No filename. */
  404.     return(0);            /* fail. */
  405.     }
  406.     zincnt = 0;                /* Initializing these couldn't hurt */
  407.     zinptr = zinbuffer;
  408.     if (n == ZSTDIO) {            /* Standard input? */
  409.     if (isatty(0)) {
  410.         ermsg("?Terminal input not allowed\n");
  411.         debug(F110," zopeni attempted input from unredirected stdin","",0);
  412.         return(0);
  413.     }
  414.     fp[ZIFILE] = stdin;
  415.     return(1);
  416.     }
  417. /*
  418.  * We open the file but waffle on the access mode we're going to use. We then
  419.  * inspect the file characteristics to see if the organization is fixed or un-
  420.  * defined. If it is, we convert to block mode operation. This is needed since
  421.  * VMS maintains a "first free byte" field to tell us how much of the last rec-
  422.  * ord really contains data, but won't terminate reads at that point. Thus, if
  423.  * we want to SEND the exact same file we RECEIVEd, we have to honor the FFB
  424.  * internally.
  425.  */
  426.     if (n == ZIFILE || n == ZRFILE) {
  427.     ifile_bmode = 0;
  428.     ifile_bcount = 0;
  429.     fab_ifile = cc$rms_fab;
  430.     fab_ifile.fab$b_fac = FAB$M_BRO | FAB$M_GET;
  431. /*
  432.  * Some non-VMS DECnet implementations don't allow switching modes, so set
  433.  * block-I/O only mode if the user said SET FILE TYPE IMAGE or LABELED.
  434.  */
  435.     if (binary == XYFT_I || binary == XYFT_L)
  436.         fab_ifile.fab$b_fac = FAB$M_BIO | FAB$M_GET;
  437.     fab_ifile.fab$l_fna = name;
  438.     fab_ifile.fab$b_fns = strlen(name);
  439.     fab_ifile.fab$l_xab = (char *)&xabdat_ifile;
  440.     rab_ifile = cc$rms_rab;
  441.     rab_ifile.rab$l_fab = &fab_ifile;
  442.     xabdat_ifile = cc$rms_xabdat;
  443.     xabdat_ifile.xab$l_nxt = (char *)&xabfhc_ifile;
  444.     xabfhc_ifile = cc$rms_xabfhc;
  445.     xabfhc_ifile.xab$l_nxt = (char *)&xabpro_ifile;
  446.     xabpro_ifile = cc$rms_xabpro;
  447.     memset(&aclbuf, 0, sizeof(aclbuf));
  448.     xabpro_ifile.xab$l_aclsts = SS$_NORMAL;        /* Oh Joy! DECnet */
  449.     xabpro_ifile.xab$l_aclbuf = (char *)&aclbuf;
  450.     xabpro_ifile.xab$w_aclsiz = sizeof(aclbuf);
  451.     xabpro_ifile.xab$l_aclctx = 0;
  452.     xabpro_ifile.xab$l_nxt = (char *)&xaball_ifile;
  453.     xaball_ifile = cc$rms_xaball;
  454.  
  455.     rms_sts = sys$open(&fab_ifile);
  456.     if (rms_sts != RMS$_NORMAL) {
  457.         debug(F101," zopeni $open failed, status","",rms_sts);
  458.         debug(F101," zopeni $open failed, stv","",fab_ifile.fab$l_stv);
  459.         return(0);
  460.     }
  461.     if (!(xabpro_ifile.xab$l_aclsts & 1)) {
  462.         if (xabpro_ifile.xab$l_aclsts != SS$_ACLEMPTY) {
  463.         debug(F101," zopeni $open ACL failed, status","",
  464.           xabpro_ifile.xab$l_aclsts);
  465.         return(0);
  466.         }
  467.     }
  468. /*
  469.  * We have the file opened. See if it's fixed or undefined format...
  470.  */
  471.     if (fab_ifile.fab$b_rfm == FAB$C_UDF) {
  472.         debug(F100," zopeni undefined file format - using blk I/O","",0);
  473.         ifile_bmode = 1;
  474.     }
  475.     if (fab_ifile.fab$b_rfm == FAB$C_FIX) {
  476.         if ((fab_ifile.fab$b_rat & (FAB$M_FTN | FAB$M_CR | FAB$M_PRN))
  477.         == 0) {
  478.         debug(F100," zopeni fixed file format - using blk I/O","",0);
  479.         ifile_bmode = 1;
  480.         }
  481.       }
  482.     debug(F101," zopeni binary flag at open","",binary);
  483.     if (binary == XYFT_I) {
  484.         debug(F100," zopeni using IMAGE mode by user request","",0);
  485.         ifile_bmode = 1;
  486.     }
  487.     if (binary == XYFT_L) {
  488.         debug(F100," zopeni using LABELED mode by user request","",0);
  489.         ifile_bmode = 2;
  490.     }
  491.     rab_ifile.rab$l_rop = 0;
  492.     rms_sts = sys$connect(&rab_ifile);
  493.     if (rms_sts != RMS$_NORMAL) {
  494.         debug(F101," zopeni $connect failed, status","",rms_sts);
  495.         return(0);
  496.     }
  497.     debug(F100," zopeni RMS operations completed ok","",0);
  498.     fp[n] = fopen("NLA0:","r");    /* it wants a fp, give it one */
  499.     zincnt = 0;            /* reset input buffer */
  500.     if (binary == XYFT_L)
  501.         do_label_send(name);    /* make a file label */
  502.     return(1);
  503.     }
  504.     zincnt = 0;                /* Reset input buffer */
  505.     fp[n] = fopen(name,"r");        /* Real file. */
  506.     debug(F111," zopeni", name, (int) fp[n]);
  507.     if (fp[n] == NULL) perror("zopeni");
  508.     return((fp[n] != NULL) ? 1 : 0);
  509. }
  510.  
  511. /*  Z O P E N O  --  Open a new file for output.  */
  512.  
  513. int
  514. zopeno(n,name,zz,fcb)
  515.     int n; char *name; struct zattr *zz; struct filinfo *fcb; {
  516.  
  517. /* As of Version 5A, the attribute structure and the file information */
  518. /* structure are included in the arglist. */
  519.  
  520.     int fildes;
  521.  
  522.     if (n != ZDFILE)
  523.       debug(F111," zopeno",name,n);
  524.     if (chkfn(n) != 0) return(0);
  525.  
  526.     zoutcnt = 0;        /* (PWP) reset output buffer */
  527.     zoutptr = zoutbuffer;
  528.     cflag = 0;            /* default to not using console */
  529.  
  530. /*
  531.  * Open Terminal or STDIO
  532.  */
  533.  
  534.     if ((n == ZCTERM) || (n == ZSTDIO)) {
  535.     fp[ZOFILE] = stdout;
  536.     cflag = 1;        /* say using console */
  537.     if (n != ZDFILE)
  538.         debug(F101," fp[]=stdout", "", (int) fp[n]);
  539.     return(1);
  540.     }
  541.  
  542. /*
  543.  * Open Debug, Transaction, Packet, Session logfile, or a Write file
  544.  * The only other possibility at this point is the output file, so we test that
  545.  */
  546.  
  547.     if (n != ZOFILE) {
  548.     if (n != ZSFILE)
  549.         fildes = creat(name, 0, "rat=cr", "rfm=var", "mrs=80");
  550.     else
  551.         fildes = creat(name, 0, "ctx=stm");
  552.     fp[n] = (fildes == -1) ? NULL : fdopen(fildes, "w");
  553.     if (fp[n] == NULL)
  554.         perror(name);
  555.     return((fp[n] != NULL) ? 1 : 0);
  556.     }
  557.  
  558. /*
  559.  * Open a file to store data being RECEIVEd
  560.  */
  561.  
  562.     if (n == ZOFILE) {
  563.     switch (binary) {
  564.         case XYFT_T:
  565.         debug(F100," zopeno receiving TEXT file","",0);
  566.         break;
  567.         case XYFT_B:
  568.         debug(F100," zopeno receiving BINARY file","",0);
  569.         break;
  570.         case XYFT_I:
  571.         debug(F100," zopeno receiving IMAGE file-program bug!","",0);
  572.         break;
  573.         case XYFT_L:
  574.         debug(F100," zopeno receiving LABELED file","",0);
  575.         break;
  576.         case XYFT_U:
  577.         debug(F100," zopeno receiving UNDEFINED file","",0);
  578.         break;
  579.         default:
  580.         debug(F101," zopeno unknown file type","",binary);
  581.     }
  582.     ofile_bmode = binary;
  583.     ofile_dump = 0;
  584.     ofile_ffb = -1;
  585.     fab_ofile = cc$rms_fab;
  586.     fab_ofile.fab$l_fna = name;
  587.     fab_ofile.fab$b_fns = strlen(name);
  588.     fab_ofile.fab$l_fop = FAB$M_MXV;
  589.     if (ofile_bmode) {
  590.         fab_ofile.fab$b_fac = FAB$M_BIO;
  591.         debug(F101," zopeno using record size","",frecl);
  592.         fab_ofile.fab$w_mrs = frecl;
  593.         if (ofile_bmode == XYFT_U)
  594.           fab_ofile.fab$b_rfm = FAB$C_UDF;
  595.         else
  596.           fab_ofile.fab$b_rfm = FAB$C_FIX;
  597.     } else {
  598.         fab_ofile.fab$b_rat = FAB$M_CR;
  599.         fab_ofile.fab$b_rfm = FAB$C_VAR;
  600.     }
  601.     fab_ofile.fab$b_shr = FAB$M_NIL;
  602.     fab_ofile.fab$l_xab = (char *)&xabdat_ofile;
  603.     rab_ofile = cc$rms_rab;
  604.     rab_ofile.rab$l_fab = &fab_ofile;
  605.     xabdat_ofile = cc$rms_xabdat;
  606.     xabdat_ofile.xab$l_nxt = (char *)&xabfhc_ofile;
  607.     xabfhc_ofile = cc$rms_xabfhc;
  608.     if (zz)
  609.       zstime(name, zz, 2);        /* set creation date */
  610.     if (ofile_bmode == XYFT_L) {    /* defer open if labeled */
  611.         ofile_lblproc = 0;        /* haven't processed labels yet */
  612.         ofile_lblopts = fcb->lblopts;
  613.         debug(F101," zopeno lblopts","",ofile_lblopts);
  614.         debug(F100," zopeno RMS operations deferred","",0);
  615.     } else {
  616.         rms_sts = sys$create(&fab_ofile);
  617.         if (!(rms_sts & 1)) {
  618.         debug(F101," zopeno $create failed, status","",rms_sts);
  619.         return(0);
  620.         }
  621.         rms_sts = sys$connect(&rab_ofile);
  622.         if (rms_sts != RMS$_NORMAL) {
  623.         debug(F101," zopeno $connect failed, status","",rms_sts);
  624.         return(0);
  625.         }
  626.         debug(F100," zopeno RMS operations completed ok","",0);
  627.     }
  628.     fp[n] = fopen("NLA0:","r");    /* it wants a fp, give it one */
  629.     return(1);
  630.     }
  631. }
  632.  
  633. /*  Z C L O S E  --  Close the given file.  */
  634.  
  635. /*  Returns 0 if arg out of range, 1 if successful, -1 if close failed.  */
  636.  
  637. int
  638. zclose(n) int n; {
  639.     int x=0;
  640.  
  641.     debug(F101," zclose","",n);
  642.     if (chkfn(n) < 1) return(0);
  643.  
  644. /* If this is the subprocess file, close it to flush output */
  645.  
  646.     if ((n == ZIFILE || (n == ZRFILE)) && (subprocess_input != 0)) {
  647.         debug(F100, "zclose calling zclosf", "", 0);
  648.     return (zclosf(n));
  649.     }
  650.  
  651. /* Input file */
  652.  
  653.     if (n == ZIFILE) {
  654.     rms_sts = sys$close(&fab_ifile);
  655.     if (rms_sts != RMS$_NORMAL) {
  656.         debug(F101," zclose $close failed, status","",rms_sts);
  657.         return(-1);
  658.     }
  659.     debug(F100," zclose RMS operations completed ok","",0);
  660.     x = fclose(fp[n]);        /* close the dummy file */
  661.     fp[n] = NULL;            /* and mark it so */
  662.     iflen = -1;
  663.     return(1);
  664.     }
  665.  
  666. /* Output file */
  667. /* This can probably be combined with the ZIFILE stuff later */
  668.  
  669.     if (n == ZOFILE) {
  670.     ofile_dump = 1;                /* force complete dump */
  671.     while (zoutcnt != 0) {
  672.         rms_sts = zoutdump();        /* flush buffers to disk */
  673.         if (rms_sts != 0)
  674.         return(-1);            /* in case of error */
  675.     }
  676.  
  677.     if (ofile_bmode == XYFT_L) { /* update revisions if labeled */
  678.         debug(F100," zclose updated labeled revision count","",0);
  679.         memmove(&xabrdt_ofile.xab$q_rdt, revdat, 8);
  680.         memmove(&xabrdt_ofile.xab$w_rvn, &revnum, 2);
  681.     }
  682.  
  683.     if (cflag != 1) {
  684.         rms_sts = sys$close(&fab_ofile);
  685.         if (rms_sts != RMS$_NORMAL) {
  686.         debug(F101," zclose $close failed, status","",rms_sts);
  687.         return(-1);
  688.         }
  689.         x = fclose(fp[n]);            /* close the dummy file */
  690.     } else {
  691.         cflag = 0;
  692.     }
  693.     debug(F100," zclose RMS operations completed ok","",0);
  694.     fp[n] = NULL;                /* and mark it so */
  695.     iflen = -1;
  696.     return(1);
  697.     }
  698.  
  699.     /* Other kind of file */
  700.  
  701.     if ((fp[n] != stdout) && (fp[n] != stdin))
  702.       x = fclose(fp[n]);
  703.     fp[n] = NULL;
  704.     iflen = -1;                    /* Invalidate file length */
  705.     debug(F101,"  x","",x);
  706.     if (x == EOF)                /* if we got a close error */
  707.       return (-1);
  708.     else
  709.       return (1);
  710. }
  711.  
  712. int
  713. get_subprc_line() {
  714.     struct iosb_struct subiosb;
  715.  
  716.     unsigned int sts;
  717. /*
  718.  * Someone complained that subprocess deletion would hang the Kermit server.
  719.  * This can be triggered by sending something silly like REMOTE HOST STOP/ID=0.
  720.  * If SUPERSAFE is defined we will check to make sure the subprocess still
  721.  * exists before every read from the mailbox. This will slow things down a bit,
  722.  * but should stop the "C-Kermit just dies" reports.
  723.  */
  724. #ifdef    SUPERSAFE
  725.     unsigned short pid;
  726.  
  727.     struct itmlstdef {
  728.     short int buflen;
  729.     short int itmcod;
  730.     char *bufaddr;
  731.     long int *retlen;
  732.     };
  733.     struct itmlstdef itmlst[] = {
  734.     4, JPI$_PID, (char *)&pid, 0,
  735.     0, 0, 0, 0
  736.     };
  737.     sts = sys$getjpiw(0, &sub_pid, 0, &itmlst, 0, 0, 0);
  738.  
  739.     debug(F101,"get_subprc_line sys$getjpiw status", "", sts);
  740.     if (sts == SS$_NONEXPR)
  741.     return(-1);
  742. #endif    /* SUPERSAFE */
  743.  
  744.     sts = sys$qiow(QIOW_EFN, mbx_chan, IO$_READVBLK, &subiosb, 0, 0, sub_buf,
  745.      sizeof(sub_buf), 0, 0, 0, 0);
  746.  
  747.     debug(F101,"get_subprc_line sys$qiow status", "", sts);
  748.     if (sts != SS$_NORMAL)
  749.     return(-1);
  750.  
  751.     debug(F101,"get_subprc_line sys$qiow subiosb.status", "", subiosb.status);
  752.     if (subiosb.status == SS$_ENDOFFILE)
  753.     return(-1);
  754.  
  755.     if (subiosb.status != SS$_NORMAL)
  756.     return(-1);
  757.  
  758.     sub_buf[subiosb.size] = '\r';
  759.     sub_buf[subiosb.size + 1] = '\n';
  760.     sub_buf[subiosb.size + 2] = '\0';
  761.     sub_count = subiosb.size + 2;
  762.     sub_ptr = sub_buf;
  763.  
  764.     return(0);
  765. }
  766.  
  767. /*  Z C H I N  --  Get a character from the input file.  */
  768.  
  769. /*  Returns -1 if EOF, 0 otherwise with character returned in argument  */
  770.  
  771. int
  772. zchin(n,c) int n, *c; {
  773.     int a;
  774.  
  775. #ifdef DEBUG
  776.     if (chkfn(n) < 1) return(-1);
  777. #endif
  778.  
  779.     if (n == ZIFILE && subprocess_input) {
  780.     if (--sub_count < 0)
  781.         if (get_subprc_line()) return(-1);
  782.     a = *sub_ptr++;
  783.     } else {
  784.     a = zminchar();
  785.     }
  786.     if (a == EOF) return(-1);
  787.     *c = (unsigned char)a;
  788.     return(0);
  789. }
  790.  
  791. /*  Z S I N L  --  Read a line from a file.  */
  792.  
  793. /*
  794.   Writes the line into the address provided by the caller.
  795.   n is the Kermit "channel number".
  796.   Writing terminates when newline is encountered, newline is not copied.
  797.   Writing also terminates upon EOF or if length x is exhausted.
  798.   Returns 0 on success, -1 on EOF or error.
  799. */
  800.  
  801. int
  802. zsinl(n,s,x) int n, x; char *s; {
  803.     int a, z = 0;
  804.     int old;
  805.  
  806.     if (chkfn(n) < 1) {            /* Make sure file is open */
  807.     return(-1);
  808.     }
  809.     a = -1;
  810.     while (x--) {
  811.     old = a;            /* Previous character */
  812.     if (zchin(n,&a) < 0)        /* Read a character from the file */
  813.         return(-1);            /* Signal EOF if problem */
  814.     a = a & 0377;
  815. #ifdef NLCHAR
  816.     if (a == (char) NLCHAR) break;    /* Single-character line terminator */
  817. #else
  818.     if (a == '\r') continue;    /* CRLF line terminator */
  819.     if (old == '\r') {
  820.         if (a == '\n') break;
  821.         else *s++ = '\r';
  822.     }
  823. #endif /* NLCHAR */
  824.     *s = a;
  825.     s++;
  826.     }
  827.     *s = '\0';
  828.     return(z);
  829. }
  830.  
  831. /*  Z I N F I L L  --  Read a line from a file.  */
  832.  
  833. /*
  834.  * (re)fill the buffered file input buffer with data.  All file input
  835.  * should go through this routine, usually by calling the zminchar()
  836.  * macro (defined in ckcker.h).
  837.  */
  838.  
  839. int
  840. zinfill() {
  841.     char cchar;
  842.     int linelen;
  843.  
  844.     if (subprocess_input) {
  845.     if (get_subprc_line()) return(-1);
  846.  
  847. /*
  848.  * The size problem should never happen.  sub_buf of a size greater then
  849.  * 1k is highly unlikely to be needed.
  850.  */
  851.  
  852.     if (INBUFSIZE < SUB_BUF_SIZE) {
  853.         fprintf(stderr,"zinfill: sub_buf too large for zinbuffer");
  854.         exit();
  855.     }
  856.     zinptr = sub_buf;
  857.     zincnt = sub_count;
  858.     } else {
  859.     if (ifile_bmode != 0) {
  860.         rab_ifile.rab$l_rop = RAB$M_BIO;    /* block mode I/O */
  861. #ifdef DYNAMIC
  862.         rab_ifile.rab$l_ubf = zinbuffer;
  863. #else
  864.         rab_ifile.rab$l_ubf = &zinbuffer;
  865. #endif
  866.         rab_ifile.rab$w_usz = 512;
  867.         rms_sts = sys$read(&rab_ifile);
  868.         if (rms_sts == RMS$_EOF)
  869.         return(-1);            /* end of file */
  870.         if (rms_sts != RMS$_NORMAL) {
  871.         debug(F101," zinfill $read failed, status","",rms_sts);
  872.         return(-1);            /* fatal */
  873.         }
  874.         ifile_bcount++;            /* say another block read */
  875.         zincnt = 512;
  876.         if (ifile_bcount == xabfhc_ifile.xab$l_ebk) {
  877.         if (ifile_bmode == 1)        /* BINARY but not LABELED */
  878.             zincnt = xabfhc_ifile.xab$w_ffb;
  879.         }
  880.         zinptr = zinbuffer;
  881.         zincnt--;            /* one less char in buffer */
  882.         return((int)(*zinptr++) & 0377); /* because we return the first */
  883.     }
  884.     if (fab_ifile.fab$b_rat & FAB$M_FTN) {
  885. #ifdef DYNAMIC
  886.         rab_ifile.rab$l_ubf = zinbuffer+2;
  887. #else
  888.         rab_ifile.rab$l_ubf = &zinbuffer+2;
  889. #endif
  890.         rab_ifile.rab$w_usz = INBUFSIZE-4;    /* space for carriage ctl */
  891.     } else {
  892. #ifdef DYNAMIC
  893.         rab_ifile.rab$l_ubf = zinbuffer;
  894. #else
  895.         rab_ifile.rab$l_ubf = &zinbuffer;
  896. #endif
  897.         rab_ifile.rab$w_usz = INBUFSIZE-2;    /* space for possible CR/LF */
  898.     }
  899.     rab_ifile.rab$l_rop = 0;        /* doing record I/O */
  900.     rms_sts = sys$get(&rab_ifile);
  901.     if (rms_sts == RMS$_EOF)
  902.         return(-1);                /* end of file */
  903.     if (rms_sts != RMS$_NORMAL) {
  904.         debug(F101," zinfill $get failed, status","",rms_sts);
  905.        return(-1);                /* fatal */
  906.     }
  907.  
  908. /*
  909.  * Do assorted contortions with Fortran carriage control to make it formatted
  910.  * ASCII instead, since many systems don't know about Fortran format in files.
  911.  */
  912.  
  913.     if (fab_ifile.fab$b_rat & FAB$M_FTN) {
  914.         linelen = rab_ifile.rab$w_rsz-1;    /* sans control code */
  915.         cchar = zinbuffer[2];        /* control code */
  916.         switch (cchar) {
  917.         case '\0':            /* data<CR> */
  918.         case '+':
  919.             zinbuffer[linelen+3] = '\r';/*  insert return */
  920.             zinptr = zinbuffer+3;
  921.             zincnt = linelen+1;        /* count it */
  922.             break;
  923.         case '$':            /* <LF>data<CR> */
  924.         case ' ':
  925.             zinbuffer[2] = '\n';    /*  insert newline */
  926.             zinbuffer[linelen+3] = '\r';/*  insert return */
  927.             zinptr = zinbuffer+2;
  928.             zincnt = linelen+2;        /*  count 'em */
  929.             break;
  930.         case '0':            /* <LF><CR><LF>data<CR> */
  931.             zinbuffer[0] = '\n';    /*  insert 1st newline */
  932.             zinbuffer[1] = '\r';    /*  insert 1st return */
  933.             zinbuffer[2] = '\n';    /*  insert 2nd newline */
  934.             zinbuffer[linelen+3] = '\r';/*  insert 2nd return */
  935.             zinptr = zinbuffer;
  936.             zincnt = linelen+4;        /*  count 'em */
  937.             break;
  938.         case '1':            /* <FF>data<CR> */
  939.             zinbuffer[2] = '\f';    /*  insert formfeed */
  940.             zinbuffer[linelen+3] = '\r';/*  insert return */
  941.             zinptr = zinbuffer+2;
  942.             zincnt = linelen+2;        /*  count 'em */
  943.             break;
  944.         default:            /* <LF>data<CR> */
  945.             zinbuffer[2] = '\n';    /*  insert newline */
  946.             zinbuffer[linelen+3] = '\r';/*  insert return */
  947.             zinptr = zinbuffer+2;
  948.             zincnt = linelen+2;        /*  count 'em */
  949.             break;
  950.         }
  951.     } else {
  952.         zincnt = rab_ifile.rab$w_rsz;
  953.         zinptr = zinbuffer;            /* reset pointer */
  954.     }
  955.  
  956. /*
  957.  * Here we see if we need to insert CR/LF pairs at the record boundary. For
  958.  * the moment, we will add them if the file has "carriage return carriage
  959.  * control" when looked at by a DIRECTORY command. As of edit 036 we also do
  960.  * this for "print file carriage control" files. I'm open to comments de-
  961.  * scribing cases where this doesn't work...
  962.  */
  963.  
  964.     if (fab_ifile.fab$b_rat & (FAB$M_CR | FAB$M_PRN)) {
  965.         zinbuffer[zincnt] = '\r';
  966.         zinbuffer[zincnt + 1] = '\n';
  967.         zincnt += 2;
  968.     }
  969.     }
  970.     zincnt--;            /* one less char in buffer */
  971.     return((int)(*zinptr++) & 0377); /* because we return the first */
  972. }
  973.  
  974.  
  975. /*  Z S O U T  --  Write a string to the given file, buffered.  */
  976.  
  977. int
  978. zsout(n,s) int n; char *s; {
  979. #ifdef DEBUG
  980.     if (chkfn(n) < 1) return(-1);
  981. #endif
  982.     fputs(s, fp[n]);            /* Don't use fprintf here MM */
  983.     return(0);
  984. }
  985.  
  986.  
  987. /*  Z S O U T L  --  Write string to file, with line terminator, buffered.  */
  988.  
  989. int
  990. zsoutl(n,s) int n; char *s; {
  991. #ifdef DEBUG
  992.     if (chkfn(n) < 1) return(-1);
  993. #endif
  994.     fputs(s, fp[n]);            /* Don't use fprintf MM */
  995.     putc('\n', fp[n]);
  996.     return(0);
  997. }
  998.  
  999.  
  1000. /*  Z S O U T X  --  Write x characters to file, unbuffered.  */
  1001.  
  1002. int
  1003. zsoutx(n,s,x) int n, x; char *s; {
  1004. #ifdef DEBUG
  1005.     if (chkfn(n) < 1) return(-1);
  1006. #endif
  1007.     return(write(fileno(fp[n]),s,x));
  1008. }
  1009.  
  1010.  
  1011. /*  Z C H O U T  --  Add a character to the given file.  */
  1012.  
  1013. int
  1014. #ifdef CK_ANSIC
  1015. zchout(register int n, char c)
  1016. #else
  1017. zchout(n,c) register int n; char c;
  1018. #endif /* CK_ANSIC */
  1019. /* zchout */ {
  1020. #ifdef DEBUG
  1021.     if (chkfn(n) < 1) return(-1);
  1022. #endif
  1023.     if (n == ZSFILE) {
  1024.         return(write(fileno(fp[n]),&c,1)); /* Use unbuffered for session log */
  1025.     } else {
  1026.     if (putc(c,fp[n]) == EOF)    /* If true, maybe there was an error */
  1027.       return(ferror(fp[n]) ? -1 : 0); /* Check to make sure */
  1028.     else                /* Otherwise... */
  1029.       return(0);            /* There was no error. */
  1030.     }
  1031. }
  1032.  
  1033. /*  Z O U T D U M P  --  dump buffered output characters to file.  */
  1034.  
  1035. /* Buffered file output, buffer dump */
  1036.  
  1037. /*
  1038.  * No, this isn't an entry in the 199x Obfuscated C programming contest, nor
  1039.  * did we get it at an all-night convenience store. VMS requires that stream
  1040.  * format files be written as records, so we have to do _lots_ of contortion
  1041.  * to make sure we write whole lines as records. Not pretty.
  1042.  */
  1043.  
  1044. int
  1045. zoutdump() {
  1046.     int ocnt;
  1047.     int wrote_one_line = 0;
  1048.     CHAR *optr, *srcptr, *endptr;
  1049.     char csave;
  1050.  
  1051.     debug(F101," zoutdump zoutcnt","",zoutcnt);
  1052.     debug(F101," zoutdump ofile_bmode","",ofile_bmode);
  1053.  
  1054. /*
  1055.  * Well, this could be to the console. If it is, chop it into itty-bitty parts
  1056.  * (the VMS CRTL can't handle a %s spec bigger than 512 bytes) and print it.
  1057.  */
  1058.  
  1059.     if (cflag == 1) {            /* If we're dumping to console */
  1060.     endptr = zoutbuffer + zoutcnt;
  1061.     for (optr = zoutbuffer; optr < endptr; optr += 511) {
  1062.         if (optr+511 < endptr) {    /* More than 511, break up */
  1063.         csave = *(optr+511);
  1064.         *(optr+511) = '\0';
  1065.         printf("%s", optr);
  1066.         *(optr+511) = csave;
  1067.         }
  1068.         else {
  1069.         *endptr = '\0';        /* Make sure null-terminated */
  1070.         printf("%s", optr);
  1071.         }
  1072.     }
  1073.     zoutcnt = 0;
  1074.     zoutptr = zoutbuffer;
  1075.     return(0);
  1076.     }
  1077.  
  1078. /*
  1079.  * Do we need to processed TYPE LABELED contortions?
  1080.  */
  1081.  
  1082.     if (ofile_bmode == XYFT_L) {    /* Is it labeled? */
  1083.     if (ofile_lblproc == 0)    {    /* I've never gone this way before? */
  1084.         rms_sts = do_label_recv();    /* Beyond revolving rainbow door... */
  1085.         if (rms_sts == -1)
  1086.         return(-1);        /* Got a hard error in label proc. */
  1087.         if (rms_sts == 1 && ofile_dump != 1)
  1088.         return(0);        /* Exit so we can fill up the buffer */
  1089.     }
  1090.     }
  1091.  
  1092. /*
  1093.  * Well, we could be lucky...
  1094.  */
  1095.  
  1096.     if (zoutcnt == 0)
  1097.     return(0);
  1098.  
  1099. /*
  1100.  * Oh well. See if doing binary - that's easy...
  1101.  */
  1102.  
  1103.     if (ofile_bmode) {
  1104.     if (zoutcnt == OBUFSIZE) {
  1105. #ifdef DYNAMIC
  1106.         rab_ofile.rab$l_rbf = zoutbuffer;
  1107. #else
  1108.         rab_ofile.rab$l_rbf = &zoutbuffer;
  1109. #endif /* DYNAMIC */
  1110.         rab_ofile.rab$w_rsz = OBUFSIZE;
  1111.         if (ofile_ffb != -1 && ofile_dump == 1) {
  1112.         /*
  1113.          * Only do this when doing _last_ file segment.
  1114.          */
  1115.         xabfhc_ofile.xab$w_ffb = ofile_ffb;
  1116.         if (ofile_ffb)
  1117.             rab_ofile.rab$w_rsz -= (512 - ofile_ffb);
  1118.         debug(F101," zoutdump ofile_ffb","",(int)ofile_ffb);
  1119.         debug(F101," zoutdump rab$w_rsz","",rab_ofile.rab$w_rsz);
  1120.         }
  1121.         rms_sts = sys$write(&rab_ofile);
  1122.         if (rms_sts != RMS$_NORMAL) {
  1123.         debug(F101," zoutdump $write failed, status","",rms_sts);
  1124.         return(-1);
  1125.         }
  1126.     } else {
  1127. #ifdef DYNAMIC
  1128.         rab_ofile.rab$l_rbf = zoutbuffer;
  1129. #else
  1130.         rab_ofile.rab$l_rbf = &zoutbuffer;
  1131. #endif
  1132.         rab_ofile.rab$w_rsz = zoutcnt;
  1133.         xabfhc_ofile.xab$w_ffb = (zoutcnt & 511)+1;
  1134.         if (ofile_ffb != -1) {
  1135.         xabfhc_ofile.xab$w_ffb = ofile_ffb;
  1136.         if (ofile_ffb)
  1137.             rab_ofile.rab$w_rsz -= (512 - ofile_ffb);
  1138.         debug(F101," zoutdump ofile_ffb","",(int)ofile_ffb);
  1139.         debug(F101," zoutdump rab$w_rsz","",rab_ofile.rab$w_rsz);
  1140.         }
  1141.         rms_sts = sys$write(&rab_ofile);
  1142.         if (rms_sts != RMS$_NORMAL) {
  1143.         debug(F101," zoutdump $write failed, status","",rms_sts);
  1144.         return(-1);
  1145.         }
  1146.     }
  1147.     debug(F100," zoutdump RMS operations completed ok","",0);
  1148.     zoutcnt = 0;
  1149.     zoutptr = zoutbuffer;
  1150.     return(0);
  1151.     }
  1152.  
  1153. /*
  1154.  * Must be ASCII. This is harder, and weirder... It's actually easier than
  1155.  * it looks, but there's (unfortunately) no really easy way to _implement_
  1156.  * it. (sigh, whimper, groan)
  1157.  */
  1158.  
  1159.     srcptr = zoutbuffer;            /* Points to first line in buffer */
  1160.     endptr = zoutbuffer + zoutcnt;  /* Points to location after last char */
  1161. zoutdump_ascii:
  1162.     /* Scan through buffer until we find a CR or we run out of chars */
  1163.     for (optr = srcptr; optr < endptr; optr++) if (*optr == CR) break;
  1164.  
  1165.     /* If there are at least 2 chars left in the buffer when we stop   */
  1166.     /* scanning, then it is assumed the above loop terminated because  */
  1167.     /* it found the CR and that both the CR and LF are present in the  */
  1168.     /* buffer (situation normal.                                       */
  1169.     /* If there are not 2 chars left in the buffer, we have one of two */
  1170.     /* cases which we treat identically:                               */
  1171.     /*  1) If there are 0 chars left in the buffer, then the line's    */
  1172.     /*     terminating CR LF are yet to come. So... we copy the data   */
  1173.     /*     to the front of the buffer and exit (next time it should be */
  1174.     /*     there.)                                                     */
  1175.     /*  2) If there is one char left in the buffer, we have the case of*/
  1176.     /*     a line with the CR but no LF present. So... do the same     */
  1177.     /*     because the LF will be coming next time.                    */
  1178.     if (optr+2 > endptr) {            /* drat! ran off the end */
  1179.     if (ofile_dump && (srcptr == endptr)) {
  1180.         /* If the beginning and end ptrs are the same, then there the  */
  1181.         /* is empty. Good news, 'cause we're clsoing up.               */
  1182.         zoutcnt = 0;        /* No looping, please. */
  1183.         zoutptr = zoutbuffer;
  1184.     }
  1185.     else if (ofile_dump) {        /* but it's cool, we're closing up */
  1186.         /* Oops, we've got a line with no LF and maybe no CR. Well     */
  1187.         /* write it out and exit abnormally.                           */
  1188.         rab_ofile.rab$l_rbf = srcptr;
  1189.         rab_ofile.rab$w_rsz = optr-srcptr;
  1190.         rms_sts = sys$put(&rab_ofile);
  1191.         zoutcnt = 0;
  1192.         zoutptr = zoutbuffer;
  1193.         if (rms_sts != RMS$_NORMAL) {
  1194.         debug(F101, " zoutdump $put failed, status","",rms_sts);
  1195.         return(-1);
  1196.         }
  1197.     } else if (wrote_one_line) {    /* it's still cool, we did one... */
  1198.         zoutcnt = optr - srcptr;    /* number of chars left */
  1199.         if (optr < endptr) zoutcnt++; /*[jah083] including CR if present */
  1200.         if (zoutcnt) memmove(zoutbuffer, srcptr, zoutcnt);
  1201.             /* Move'em to front of buffer*/
  1202.         zoutptr = zoutbuffer+zoutcnt;
  1203.     } else {            /* WRONG!!! */
  1204.         /* We've got a buffer full of chars with no LF (it may or may  */
  1205.         /* not have a terminating CR. In either case its just plain too*/
  1206.         /* long. I suppose we could check here for the optr+1 == endptr*/
  1207.         /* which indicates that there was a CR but no LF so we could   */
  1208.         /* issue a "line barely too long", but, is it useful?          */
  1209.         debug(F100, "zoutdump: line too long","",0);
  1210.         zoutcnt = 0;        /* No looping, please. */
  1211.         zoutptr = zoutbuffer;
  1212.         return(-1);
  1213.     }
  1214.     debug(F101, " zoutdump exiting, zoutcnt","",zoutcnt);
  1215.     return(0);
  1216.     }
  1217.  
  1218.     /* We now have a line that we can write, so... */
  1219.  
  1220.     rab_ofile.rab$l_rbf = srcptr;
  1221.     rab_ofile.rab$w_rsz = optr-srcptr;
  1222.     rms_sts = sys$put(&rab_ofile);
  1223.     if (rms_sts != RMS$_NORMAL) {
  1224.     debug(F101, " zoutdump $put failed, status","",rms_sts);
  1225.     return(-1);
  1226.     }
  1227.     srcptr = optr + 2;        /* Account for CR, LF */
  1228.     wrote_one_line = 1;
  1229.     goto zoutdump_ascii;
  1230. }
  1231.  
  1232. /*  C H K F N  --  Internal function to verify file number is ok.  */
  1233.  
  1234. /*
  1235.  Returns:
  1236.   -1: File number n is out of range
  1237.    0: n is in range, but file is not open
  1238.    1: n in range and file is open
  1239. */
  1240.  
  1241. int
  1242. chkfn(n) int n; {
  1243.     switch (n) {
  1244.     case ZCTERM:
  1245.     case ZSTDIO:
  1246.     case ZIFILE:
  1247.     case ZOFILE:
  1248.     case ZDFILE:
  1249.     case ZTFILE:
  1250.     case ZPFILE:
  1251.     case ZSFILE:
  1252.         break;
  1253.     case ZSYSFN:            /* System functions */
  1254.         return(0);
  1255.     case ZRFILE:            /* READ and WRITE files */
  1256.     case ZWFILE:
  1257.         case ZMFILE:
  1258.         break;
  1259.     default:
  1260.         debug(F101,"chkfn: file number out of range","",n);
  1261.         fprintf(stderr,"?File number out of range - %d\n",n);
  1262.         return(-1);
  1263.     }
  1264.     return( (fp[n] == NULL) ? 0 : 1 );
  1265. }
  1266.  
  1267. /*  Z C H K I  --  Check if input file exists and is readable.  */
  1268.  
  1269. /*
  1270.   Returns:
  1271.    >= 0 if the file can be read (returns the size).
  1272.      -1 if file doesn't exist or can't be accessed,
  1273.      -2 if file exists but is not readable (e.g. a directory file).
  1274.      -3 if file exists but protected against read access.
  1275. */
  1276.  
  1277. long
  1278. zchki(name) char *name; {
  1279.     struct stat buf;
  1280.     int x; long y;
  1281.  
  1282. /* This is _really_ bad. But there's a fundamental assumption in the upper
  1283.  * levels that one can call zchki() without any context to validate file-
  1284.  * names, directory names, etc. which would be painful (to the other imple-
  1285.  * mentations) to change. So, if we get an argument which ends in ':', '>',
  1286.  * or ']', we'll return an immediate OK with a size of 0. Bad directory
  1287.  * names will be caught in zchdir anyway. This has the nice side-effect that
  1288.  * saying (for example) GET dir-spec will implicitly get all files in that
  1289.  * directory. Not bad for a total kludge, huh?
  1290.  */
  1291.     x = strlen(name);
  1292.     if (name[x-1] == ':')
  1293.     return(0);
  1294.     if (name[x-1] == ']')
  1295.     return(0);
  1296.     if (name[x-1] == '>')
  1297.     return(0);
  1298.  
  1299.     fab_ifile = cc$rms_fab;
  1300.     fab_ifile.fab$b_fac = FAB$M_BIO;
  1301.     fab_ifile.fab$l_fna = name;
  1302.     fab_ifile.fab$b_fns = strlen(name);
  1303.     fab_ifile.fab$l_xab = (char *)&xabfhc_ifile;
  1304.     xabfhc_ifile = cc$rms_xabfhc;
  1305.     rms_sts = sys$open(&fab_ifile);
  1306.     if (rms_sts == RMS$_PRV)            /* No privs */
  1307.     return(-3);
  1308.     if (rms_sts != RMS$_NORMAL) {
  1309.     debug(F101," zchki $open failed, status","",rms_sts);
  1310.     return(-1);
  1311.     }
  1312.     iflen = ((xabfhc_ifile.xab$l_ebk-1)*512)+xabfhc_ifile.xab$w_ffb;
  1313.  
  1314.     rms_sts = sys$close(&fab_ifile);
  1315.     if (rms_sts != RMS$_NORMAL) {
  1316.     debug(F101," zchki $close failed, status","",rms_sts);
  1317.     return(-1);
  1318.     }
  1319.     strcpy(nambuf,name);        /* preserve name */
  1320.     debug(F111," zchki access ok:",name,(int) iflen); /* Yes */
  1321.     return( (iflen > -1) ? iflen : 0 );
  1322. }
  1323.  
  1324. /*  Z C H K O  --  Check if output file can be created.  */
  1325.  
  1326. /*
  1327.  Returns -1 if write permission for the file would be denied, 0 otherwise.
  1328. */
  1329. int
  1330. zchko(name) char *name; {
  1331.     return(0);                /* Always creates new version */
  1332. }
  1333.  
  1334. /*  Z C H K S P A  --  Check if there is enough space to store the file.  */
  1335.  
  1336. /*
  1337.  Call with file specification f, size n in bytes.
  1338.  Returns -1 on error, 0 if not enough space, 1 if enough space.
  1339. */
  1340.  
  1341. int
  1342. zchkspa(f,n) char *f; long n; {
  1343.  
  1344. /*
  1345.  * This is complicated. The user could have specified an explicit path when
  1346.  * sending the file, or could have done a CWD, or could be using the default
  1347.  * directory. If not the latter, the path may not even be a disk device, as
  1348.  * CWD LPA0: is perfect legal for uploading to the lineprinter. After that,
  1349.  * if it's a disk, we should check the user's quota.  However, the user may
  1350.  * have SYSPRV, EXQUOTA, BYPASS, or maybe even GRPPRV, and it would be hard
  1351.  * to  properly check for all these cases.  So, if the file will fit on the
  1352.  * disk, we'll accept it.
  1353.  */
  1354.  
  1355.     char   *zgtdir();
  1356.  
  1357.     struct itmlstdef {
  1358.     short int buflen;
  1359.     short int itmcod;
  1360.     char *bufaddr;
  1361.     long int *retlen;
  1362.     };
  1363.  
  1364.     static char device[64];
  1365.  
  1366.     struct dsc$descriptor_s
  1367.     dev_desc = {sizeof(device), DSC$K_DTYPE_T, DSC$K_CLASS_S,
  1368.               (char *)&device};
  1369.     unsigned long freeblocks, freelength, devclass, classlength, fileblocks;
  1370.  
  1371.     struct itmlstdef itmlst[] =
  1372.     {4,DVI$_FREEBLOCKS,0,0,4,DVI$_DEVCLASS,0,0,0,0,0,0};
  1373.  
  1374.     int rms_sts;
  1375.  
  1376. /* First, figure out the device we're interested in */
  1377.  
  1378.     strcpy(device, zgtdir());            /* Handles default or CWD */
  1379.  
  1380.     if (strchr(f, ':'))                /* If user specified path */
  1381.     strncpy(device, f, 63);
  1382.  
  1383.     debug(F110," zchkspa target device is ",device,0);
  1384.  
  1385. /* Next, ask for free block count and device type (disk vs. non-disk) */
  1386.  
  1387.     itmlst[0].bufaddr = (char *)&freeblocks;
  1388.     itmlst[0].retlen = &freelength;
  1389.     itmlst[1].bufaddr = (char *)&devclass;
  1390.     itmlst[1].retlen = &classlength;
  1391.  
  1392.     rms_sts = sys$getdviw(0,0,&dev_desc,&itmlst,0,0,0,0);
  1393.  
  1394.     debug(F101," zchkspa $getdvi returned rms_sts","",rms_sts);
  1395.  
  1396.     if (devclass != DC$_DISK)
  1397.     return(1);                /* assume space if not disk */
  1398.  
  1399.     if (rms_sts != SS$_NORMAL)
  1400.     return(1);                /* assume free space if err */
  1401.  
  1402.     debug(F101," zchkspa $getdvi returned freeblocks","",freeblocks);
  1403.  
  1404. /* Pad file size if it's a text file */
  1405.  
  1406.     if (ofile_bmode == XYFT_T)
  1407.     n += (n/40) * 3;
  1408.  
  1409.     fileblocks = n / 512 + 1;            /* compute file size in blks */
  1410.                         /* we may want some fuzz */
  1411.     if (fileblocks >= freeblocks)
  1412.     return(0);                /* Won't fit */
  1413.     else
  1414.     return(1);                /* Will fit */
  1415. }
  1416.  
  1417. /*  Z D E L E T  --  Delete the named file.  */
  1418.  
  1419. int
  1420. zdelet(name) char *name; {
  1421.     return(delete(name));
  1422. }
  1423.  
  1424. /*  Z R T O L  --  Convert remote filename into local form.  */
  1425.  
  1426. VOID
  1427. zrtol(name,name2) char *name, *name2; {
  1428.     int count = 9, vflag = 0;
  1429.     char *cp, c;
  1430.     static char *spcl_set = "_-$[]<>:.\";";
  1431.  
  1432.     for (cp=name2; c = *name; name++) {
  1433.     if (islower(c)) c = toupper(c);
  1434.     if (!isalnum(c) &&
  1435.         !strchr(spcl_set,c)) c = 'X';
  1436.     *cp++ = c;
  1437.     }
  1438.     *cp = '\0';                /* End of name */
  1439.     debug(F110," zrtol: ",name2,0);
  1440. }
  1441.  
  1442. /*  Z L T O R  --  Convert filename from local format to common form.  */
  1443.  
  1444. VOID
  1445. zltor(name,name2) char *name, *name2; {
  1446.     char *cp, *pp;
  1447.  
  1448. /*
  1449.  * Copy name to output string
  1450.  */
  1451.  
  1452.     strcpy(name2,name);
  1453.  
  1454. /*
  1455.  * Parse the filename and type, with the default filename of "X"
  1456.  */
  1457.  
  1458.     parse_fname(name2, 100, "X", PARSE_NAME|PARSE_TYPE);
  1459.     debug(F110," zltor: ",name2,0);
  1460. }
  1461.  
  1462. /*  Z C H D I R  --  Change directory.  */
  1463.  
  1464. int
  1465. zchdir(dirnam) char *dirnam; {
  1466.  
  1467.     char   *zgtdir();
  1468.     char   dir_buff[NAM$C_MAXRSS];
  1469.     int    status;
  1470.  
  1471.     if (*dirnam == '\0')
  1472.         strcpy(dirnam,getenv("HOME"));    /* default to current dir */
  1473.  
  1474.     status = chdir(dirnam);        /* change first in parent proc */
  1475.     return(status == 0);
  1476. }
  1477.  
  1478. /*  Z H O M E  --  Return pointer to user's home directory.  */
  1479.  
  1480. char *
  1481. zhome() {
  1482.     return(getenv("HOME"));
  1483. }
  1484.  
  1485. /*  Z G T D I R  --  Return pointer to user's current directory.  */
  1486.  
  1487. char *
  1488. zgtdir() {
  1489. #ifdef VMS_V40
  1490. #define    OLD_VMS
  1491. #endif
  1492. #ifdef VMS_V42
  1493. #define    OLD_VMS
  1494. #endif
  1495. #ifdef VMS_V44
  1496. #define    OLD_VMS
  1497. #endif
  1498. #ifdef VAXC023
  1499. #define    OLD_VMS
  1500. #endif
  1501. #ifdef VAXC024
  1502. #define    OLD_VMS
  1503. #endif
  1504.  
  1505. #ifdef OLD_VMS
  1506.     static char *gtdir_buf = 0;
  1507.     static char sysdisk[] = "SYS$DISK";
  1508.     char tmp_buf[NAM$C_MAXRSS+1];
  1509.     struct dsc$descriptor_s
  1510.     tmp_buf_dsc = {sizeof(tmp_buf),DSC$K_DTYPE_T,DSC$K_CLASS_S,&tmp_buf},
  1511.     sysdisk_dsc = {sizeof(sysdisk)-1,DSC$K_DTYPE_T,DSC$K_CLASS_S,&sysdisk};
  1512.     unsigned short int buf_len;
  1513.  
  1514. /*
  1515.  * Allocate buffer dynamically, first time through.  This makes the image
  1516.  * smaller.
  1517.  */
  1518.  
  1519.     if (!gtdir_buf) gtdir_buf = malloc(NAM$C_MAXRSS+1);
  1520.  
  1521. /*
  1522.  * Translate device name.
  1523.  */
  1524.  
  1525.     LIB$SYS_TRNLOG(    &sysdisk_dsc,
  1526.             &buf_len,
  1527.             &tmp_buf_dsc,
  1528.             0,
  1529.             0,
  1530.             0);
  1531.     tmp_buf[buf_len] = '\0';
  1532.     strcpy(gtdir_buf,tmp_buf);
  1533.  
  1534. /*
  1535.  * Get directory name.
  1536.  */
  1537.  
  1538.     sys$setddir(    0,      /* New dir addr */
  1539.             &buf_len, /* length addr */
  1540.             &tmp_buf_dsc);
  1541.     tmp_buf[buf_len] = '\0';
  1542.     strcat(gtdir_buf,tmp_buf);
  1543.  
  1544.     return(gtdir_buf);  /* Can't seem to make LINK find getcwd()... */
  1545.             /* (wbader: removed &) */
  1546. #else
  1547.     char *getcwd();
  1548.     char *buf;
  1549.  
  1550.     buf = cwdbuf;
  1551.     return(getcwd(buf,100));
  1552. #endif
  1553. }
  1554.  
  1555. /*  Z X C M D  --  Run a system command so its output can be read as a file. */
  1556.  
  1557. int
  1558. zxcmd(filnum, comand) int filnum; char *comand; {
  1559.     char mbxnam[21], inpchan[6] = "NLA0:";
  1560.     unsigned long sts, pid;
  1561.     int one=1;
  1562.  
  1563.     struct dsc$descriptor_s
  1564.     mbx_desc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0},
  1565.     cmd_line = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0},
  1566.     inp_desc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
  1567.  
  1568.     struct itmlstdef {
  1569.     short int buflen;
  1570.     short int itmcod;
  1571.     char *bufaddr;
  1572.     long int *retlen;
  1573.     };
  1574.  
  1575.     struct itmlstdef itmlst[] = {
  1576.     4, JPI$_PID, (char *)&pid, 0,
  1577.     0, 0, 0, 0
  1578.     };
  1579.  
  1580.     debug(F101,"zxcmd filnum", "", filnum);
  1581.     if (filnum != ZIFILE && filnum != ZRFILE)
  1582.     return(0);
  1583.  
  1584.     sts = sys$getjpiw(0, 0, 0, &itmlst, 0, 0, 0);
  1585.  
  1586.     debug(F101,"zxcmd sys$getjpiw status", "", sts);
  1587.     if (sts != SS$_NORMAL)
  1588.     return(0);
  1589.  
  1590.     sprintf(mbxnam,"KERMIT$MBX_%08X", pid);
  1591.     debug(F110,"zxcmd mailbox logical", mbxnam, 0);
  1592.  
  1593.     mbx_desc.dsc$w_length = strlen(mbxnam);
  1594.     mbx_desc.dsc$a_pointer = mbxnam;
  1595.  
  1596.     sts = sys$crembx(0, &mbx_chan, SUB_BUF_SIZE, 0, 0, 0, &mbx_desc, 0);
  1597.  
  1598.     debug(F101,"zxcmd sys$crembx status", "", sts);
  1599.     if (sts != SS$_NORMAL)
  1600.     return(0);
  1601.  
  1602.     debug(F101,"zxcmd sys$crembx mbx_chan", "", mbx_chan);
  1603.  
  1604.     strcat(mbxnam, ":");
  1605.     mbx_desc.dsc$w_length++;
  1606.  
  1607.     cmd_line.dsc$w_length = strlen(comand);
  1608.     cmd_line.dsc$a_pointer = comand;
  1609.  
  1610.     inp_desc.dsc$w_length = strlen(inpchan);
  1611.     inp_desc.dsc$a_pointer = inpchan;
  1612.  
  1613.     sts = lib$spawn(&cmd_line, &inp_desc, &mbx_desc, &one, 0, &sub_pid,
  1614.             0, 0, 0, &mbx_chan);
  1615.  
  1616.     debug(F101,"zxcmd lib$spawn status", "", sts);
  1617.     if (sts != SS$_NORMAL)
  1618.     return(0);
  1619.  
  1620.     subprocess_input = 1;
  1621.     sub_count = 0;
  1622.     fp[filnum] = fopen("NLA0:","r");    /* It wants a fp, give it one */
  1623.     debug(F101,"zxcmd fp[filnum]", "", fp[filnum]);
  1624.     fp[ZSYSFN] = fp[filnum];        /* Set ZSYSFN too, so we remember */
  1625.     return(1);
  1626. }
  1627.  
  1628. /*  Z C L O S F  - close the suprocess output file.  */
  1629.  
  1630. int
  1631. zclosf(filnum) int filnum; {
  1632.     unsigned long sts;
  1633.  
  1634.     if (subprocess_input != 0) {
  1635.     sts = sys$delprc(&sub_pid, 0);
  1636.  
  1637.     debug(F101,"zclosf sys$delprc status", "", sts);
  1638.  
  1639.     sts = sys$delmbx(mbx_chan);
  1640.  
  1641.     debug(F101,"zclosf sys$delmbx status", "", sts);
  1642.  
  1643.     sts = sys$dassgn(mbx_chan);
  1644.  
  1645.     debug(F101,"zclosf sys$dassgn status", "", sts);
  1646.  
  1647.     sub_ptr = sub_buf;            /* flush remaining data */
  1648.     sub_count = 1;
  1649.     *sub_buf = '\0';
  1650.     zincnt = 0;
  1651.  
  1652.     fclose(fp[filnum]);            /* Close the place-holders */
  1653.     fp[filnum] = fp[ZSYSFN] = NULL;
  1654.     }
  1655.     subprocess_input = 0;            /* Say we're done */
  1656.     return(1);
  1657. }
  1658.  
  1659. /*  Z X P A N D  --  Expand a wildcard string into an array of strings.  */
  1660.  
  1661. /*
  1662.   Returns the number of files that match fn1, with data structures set up
  1663.   so that first file (if any) will be returned by the next znext() call.
  1664. */
  1665. int
  1666. zxpand(fn) char *fn; {
  1667.     if (strlen(fn) == 0)        /* Nothing asked for, */
  1668.       return(0);            /* nothing returned. */
  1669.     fcount = fgen(fn,mtchs,MAXWLD);    /* Look up the file. */
  1670.     if (fcount > 0) {
  1671.     mtchptr = mtchs;        /* Save pointer for next. */
  1672.     debug(F111," zxpand",mtchs[0],fcount);
  1673.     }
  1674.     return(fcount);
  1675. }
  1676.  
  1677. /*  Z N E X T  --  Get name of next file from list created by zxpand(). */
  1678.  
  1679. /*
  1680.  Returns >0 if there's another file, with its name copied into the arg string,
  1681.  or 0 if no more files in list.
  1682. */
  1683. int
  1684. znext(fn) char *fn; {
  1685.  
  1686.     if (fcount-- > 0) strcpy(fn,*mtchptr++);
  1687.     else *fn = '\0';
  1688.     debug(F111," znext",fn,fcount+1);
  1689.     return(fcount+1);
  1690. }
  1691.  
  1692. /*  Z N E W N  --  Make a new name for the given file.  */
  1693.  
  1694. VOID
  1695. znewn(fn,s) char *fn, **s; {
  1696.     static char buf[NAM$C_MAXRSS];
  1697.  
  1698.     strcpy(buf, fn);            /* Version numbers are handled by OS */
  1699.     *s = buf;
  1700. }
  1701.  
  1702. /*
  1703.  * fgen:
  1704.  *  This is the actual name generator.  It is passed a string,
  1705.  *  possibly containing wildcards, and an array of character pointers.
  1706.  *  It finds all the matching filenames and stores them into the array.
  1707.  *  The returned strings are allocated from a static buffer local to
  1708.  *  this module (so the caller doesn't have to worry about deallocating
  1709.  *  them); this means that successive calls to fgen will wipe out
  1710.  *  the results of previous calls.  This isn't a problem here
  1711.  *  because we process one wildcard string at a time.
  1712.  *
  1713.  * Input: a wildcard string, an array to write names to, the
  1714.  *        length of the array.
  1715.  * Returns: the number of matches.  The array is filled with filenames
  1716.  *          that matched the pattern.  If there wasn't enough room in the
  1717.  *        array, -1 is returned.
  1718.  */
  1719. int
  1720. fgen(pat,resarry,len) char *pat,*resarry[]; int len; {
  1721.     struct dsc$descriptor_s
  1722.     file_spec = {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0},
  1723.     result = {0,DSC$K_DTYPE_T,DSC$K_CLASS_D,0},
  1724.     deflt = {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0};
  1725.     unsigned long context = 0, status;
  1726.     int count = 0;
  1727.     char *def_str = "*.*";
  1728.  
  1729.     file_spec.dsc$w_length  = strlen(pat);
  1730.     file_spec.dsc$a_pointer = pat;
  1731.  
  1732.     deflt.dsc$w_length  = sizeof(def_str)-1;
  1733.     deflt.dsc$a_pointer = def_str;
  1734.  
  1735.     while (count < len
  1736.        && (status = LIB$FIND_FILE(&file_spec, &result, &context, &deflt))
  1737.         == RMS$_NORMAL) {
  1738.         resarry[count] = malloc(result.dsc$w_length + 1);
  1739.     strncpy(resarry[count], result.dsc$a_pointer, result.dsc$w_length);
  1740.     resarry[count][result.dsc$w_length] = '\0';
  1741.     count++;
  1742.     }
  1743. #ifdef DVI$_ALT_HOST_TYPE
  1744.     LIB$FIND_FILE_END(&context);    /* Only on V4 and later */
  1745. #endif
  1746.     LIB$SFREE1_DD(&result);
  1747.     if (status == RMS$_FNF) return((count <= len) ? 0 : -1);
  1748.     if (status == RMS$_NMF) return(count);
  1749.     /* Bernd Onasch says that VMS sometimes returns RMS$_NORMAL here, so... */
  1750.     if (status == RMS$_NORMAL) return(count);
  1751.     /* Some other status.  Return 0. */
  1752.     /* Improve this later based on results from following debug stmt. */
  1753.     debug(F101,"fgen unexpected failure status","",status);
  1754.     return(0);
  1755. }
  1756.  
  1757. /*  Z R E N A M E  --  Rename a file.  */
  1758.  
  1759. /*  Call with old and new names */
  1760. /*  Returns 0 on success, -1 on failure. */
  1761. int
  1762. zrename(old,new) char *old, *new; {
  1763.     int sts;
  1764.  
  1765.     sts = rename(old,new);
  1766.  
  1767.     return((sts ? -1 : 0));
  1768. }
  1769.  
  1770. /*  Z S T I M E  --  Set or compare a file's creation date/time.  */
  1771.  
  1772. /*
  1773.  * Note: There's an additional value for parameter X on VAX/VMS systems. As
  1774.  * it's horribly painful to change a file's creation date after-the-fact we
  1775.  * call zstime with an argument  of 2 to pre-set the date when creating the
  1776.  * file. An argument of 0 (which the main-line code thinks sets the date of
  1777.  * the output file) returns success but does nothing.  Note that an invalid
  1778.  * or missing attribute packet will cause $bintim to return an error, which
  1779.  * causes the routine to exit. Since we pre-set the binary time to zero, we
  1780.  * will create the file "now", or say the incoming file is newer, whichever
  1781.  * is appropriate.
  1782.  */
  1783. int
  1784. zstime(f,yy,x) char *f; struct zattr *yy; int x; {
  1785.     int rms_sts;
  1786.     static char mth[13][4] = {    "JAN","FEB","MAR","APR",
  1787.                 "MAY","JUN","JUL","AUG",
  1788.                 "SEP","OCT","NOV","DEC",
  1789.                 ""};
  1790.     static char cdate[23];          /* Creation date yyyymmdd hh:mm:ss.00 */
  1791.     static char mnum[2];
  1792.     struct dsc$descriptor_s
  1793.     bintim_desc = {sizeof(cdate), DSC$K_DTYPE_T, DSC$K_CLASS_S,
  1794.              (char *)&cdate};
  1795.     unsigned long file_date[2], attr_date[2];
  1796.  
  1797. /* First, make a system quadword date from what we got passed */
  1798.  
  1799.     char *dptr = yy->date.val;
  1800.     if (!dptr) return(-1);
  1801.     strcpy(cdate,"dd-mmm-yyyy 00:00:00.00");
  1802.     attr_date[0]=0;                /* clear time in case of err */
  1803.     attr_date[1]=0;
  1804.     strncpy(cdate+7, dptr, 4);            /* yyyy */
  1805.     dptr += 4;
  1806.     strncpy(mnum, dptr, 2);
  1807.     strncpy(cdate+3, mth[atoi(mnum)-1], 3);    /* mm */
  1808.     dptr += 2;
  1809.     strncpy(cdate, dptr, 2);            /* dd */
  1810.     dptr += 3;
  1811.     strncpy(cdate+12, dptr, 8);            /* hhmmss */
  1812.     cdate[23] = '\0';                /* terminate */
  1813.     rms_sts = sys$bintim(&bintim_desc, &attr_date);
  1814.     if (rms_sts != SS$_NORMAL) {
  1815.     debug(F101," zstime - $bintim returns","",rms_sts);
  1816.     return(-1);
  1817.     }
  1818.     debug(F110," zstime built",cdate,0);
  1819.     sprintf(cdate, "%08X%08X", attr_date[1], attr_date[0]);
  1820.     debug(F110," $bintim attr_date", cdate, 0);
  1821.  
  1822.     if (x == 1) {
  1823.     fab_ifile = cc$rms_fab;
  1824.     fab_ifile.fab$b_fac = FAB$M_BIO | FAB$M_GET;
  1825.     fab_ifile.fab$l_fna = f;
  1826.     fab_ifile.fab$b_fns = strlen(f);
  1827.     fab_ifile.fab$l_xab = (char *)&xabdat_ifile;
  1828.     rab_ifile = cc$rms_rab;
  1829.     rab_ifile.rab$l_fab = &fab_ifile;
  1830.     xabdat_ifile = cc$rms_xabdat;
  1831.     rms_sts = sys$open(&fab_ifile);
  1832.     if (rms_sts != RMS$_NORMAL) {
  1833.         debug(F101," zstime $open failed, status","",rms_sts);
  1834.         return(-1);
  1835.     }
  1836.     memcpy(file_date, &xabdat_ifile.xab$q_cdt, 8);
  1837.     sprintf(cdate, "%08x%08x", file_date[1], file_date[0]);
  1838.     debug(F110," $bintim file_date", cdate, 0);
  1839.     rms_sts = sys$close(&fab_ifile);
  1840.     if (rms_sts != RMS$_NORMAL) {
  1841.         debug(F101," zstime $close failed, status","",rms_sts);
  1842.         return(-1);
  1843.     }
  1844.     if (attr_date[1] < file_date[1]) {
  1845.         debug(F100," zstime incoming file is older","",0);
  1846.         return(1);
  1847.     }
  1848.     if (attr_date[1] == file_date[1]) {
  1849.         if (attr_date[0] <= file_date[0]) {
  1850.         debug(F100," zstime incoming file is older, not by much","",0);
  1851.         return(1);
  1852.         }
  1853.     debug(F100," zstime incoming file is newer","",0);
  1854.     return(0);
  1855.     }
  1856.     }
  1857.  
  1858.     if (x == 0) {
  1859.     return(0);            /* say we did it (see header) */
  1860.     }
  1861.  
  1862.     if (x == 2) {
  1863.     memcpy(&xabdat_ofile.xab$q_cdt, attr_date, 8);
  1864.     return(0);            /* Set date in output file */
  1865.     }
  1866.  
  1867.     return(-1);
  1868. }
  1869.  
  1870. /*  Z K E R M I N I  --  Find initialization file.  */
  1871. /*
  1872.   Places name of init file in buffer pointed to by s.
  1873.   If no init file found, the device name of the null device is used.
  1874.   returns 0 always.
  1875. */
  1876. int
  1877. zkermini(s, s_len, def) char *s; int s_len; char *def; {
  1878.     FILE fd;
  1879.     struct dsc$descriptor_s
  1880.         dsc_in = {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0},
  1881.         dsc_out = {0,DSC$K_DTYPE_T,DSC$K_CLASS_D,0},
  1882.         dsc_def = {0,DSC$K_DTYPE_T,DSC$K_CLASS_S,0};
  1883.     int max_len;
  1884.     long unsigned int rms_s;
  1885.     unsigned long find_file_context = 0;
  1886.  
  1887.     struct TRNLIST {
  1888.     char *name;            /* ASCII file or logical name */
  1889.     unsigned char flag;        /* Zero to use default filename */
  1890.     } *p;
  1891.  
  1892.     static struct TRNLIST slist[] = {
  1893.     {"", 0},            /* Dummy first entry points to file */
  1894.     {"ckermit_ini:", 0},        /* CKERMIT_INI: points to directory */
  1895.     {"ckermit_init", 1},        /* CKERMIT_INIT points to file      */
  1896.     {"sys$login:",   0},        /* CKERMIT.INI in login directory   */
  1897.     {"", 0}
  1898.     };
  1899.     p = slist;                /* Point to list */
  1900.     if (rcflag) {            /* Name given on command line? */
  1901.     slist[0].name = def;        /* Yes, stuff its name into slist */
  1902.     slist[1].name = "";
  1903.     } else {                /* No, */
  1904.     *p++;                /* skip past dummy entry. */
  1905.     }
  1906.     while(*(p->name)) {            /* Search the list top to bottom */
  1907.  
  1908.     dsc_in.dsc$w_length = strlen(p->name); /* Length of work area */
  1909.     dsc_in.dsc$a_pointer = p->name; /* Address of string */
  1910.  
  1911.     if (!(p->flag)) {
  1912.         dsc_def.dsc$w_length = strlen(def); /* Length of work area */
  1913.         dsc_def.dsc$a_pointer = def; /* Address of string */
  1914.     } else {
  1915.         dsc_def.dsc$w_length = 0;    /* Length of work area */
  1916.         dsc_def.dsc$a_pointer = 0;    /* Address of string */
  1917.     }
  1918.     rms_s = LIB$FIND_FILE(
  1919.                 &dsc_in,    /* File spec */
  1920.                 &dsc_out,    /* Result file spec */
  1921.                 &find_file_context, /* Context */
  1922.                 &dsc_def,    /* Default file spec */
  1923.                 0,        /* Related spec */
  1924.                 0,        /* STV error */
  1925.                 0);        /* Flags */
  1926.  
  1927.     if (rms_s == RMS$_NORMAL) {
  1928.         max_len = ((unsigned short int) dsc_out.dsc$w_length < s_len ?
  1929.                (unsigned short int) dsc_out.dsc$w_length : 0);
  1930.         if (!max_len)
  1931.           fprintf(stderr,
  1932.         "%%ZKERMINI out string not long enough, ignoring .ini file\n");
  1933.         else
  1934.           strncpy(s,dsc_out.dsc$a_pointer,max_len);
  1935.         LIB$FIND_FILE_END(&find_file_context);
  1936.         LIB$SFREE1_DD(&dsc_out);    /* Return dyno memory */
  1937.         return(0);
  1938.     }
  1939.     p++;
  1940.     LIB$FIND_FILE_END(&find_file_context);
  1941.     }
  1942. /*
  1943.  * No initialization file found.  We can't return the null string because the
  1944.  * runtime library will successfully open it if the file ".;" exists in the
  1945.  * user's directory.  Instead we return the name of the null device.
  1946.  */
  1947.     strcpy(s, "NLA0:");                /* Return null init file */
  1948.     LIB$SFREE1_DD(&dsc_out);
  1949.     return(0);
  1950. }
  1951.  
  1952. static int
  1953. parse_fname(cp, cp_len, defnam, flag)
  1954. char *cp;        /* Pointer to file spec to parse */
  1955. int cp_len;        /* Length of cp field */
  1956. char *defnam;        /* Default file spec */
  1957. int flag;        /* Flag word PARSE_xxx */
  1958. {
  1959.     struct FAB fab;
  1960.     struct NAM nam;
  1961.     char expanded_name[NAM$C_MAXRSS];
  1962.     int long rms_status;
  1963.     int cur_len = 0;
  1964.  
  1965.     fab = cc$rms_fab;
  1966.     fab.fab$l_nam = &nam;
  1967.     fab.fab$l_fna = cp;
  1968.     fab.fab$b_fns = strlen(cp);
  1969.     if (defnam) {
  1970.     fab.fab$b_dns = strlen(defnam);
  1971.     fab.fab$l_dna = defnam;
  1972.     } else
  1973.     fab.fab$l_dna = 0;
  1974.  
  1975.     nam = cc$rms_nam;
  1976.     nam.nam$l_esa = (char *)&expanded_name;
  1977.     nam.nam$b_ess = sizeof(expanded_name);
  1978.  
  1979.     if (!CHECK_ERR("%%CKERMIT-W-PARSE, ",
  1980.         sys$parse(&fab)))
  1981.     return(-1);
  1982.  
  1983.     *cp = '\0';            /* Make a zero length string */
  1984.     if ((PARSE_NODE & flag) && nam.nam$b_node &&
  1985.         cur_len+nam.nam$b_node < cp_len) {
  1986.     cur_len += nam.nam$b_node;
  1987.     strncat(cp, nam.nam$l_node, (int)nam.nam$b_node);
  1988.     }
  1989.     if ((PARSE_DEVICE & flag) && nam.nam$b_dev &&
  1990.         cur_len+nam.nam$b_dev < cp_len) {
  1991.     cur_len += nam.nam$b_dev;
  1992.     strncat(cp, nam.nam$l_dev, (int)nam.nam$b_dev);
  1993.     }
  1994.     if ((PARSE_DIRECTORY & flag) && nam.nam$b_dir &&
  1995.         cur_len+nam.nam$b_dir < cp_len) {
  1996.     cur_len += nam.nam$b_dir;
  1997.     strncat(cp, nam.nam$l_dir, (int)nam.nam$b_dir);
  1998.     }
  1999.     if ((PARSE_NAME & flag) && nam.nam$b_name &&
  2000.         cur_len+nam.nam$b_name < cp_len) {
  2001.     cur_len += nam.nam$b_name;
  2002.     strncat(cp, nam.nam$l_name, (int)nam.nam$b_name);
  2003.     }
  2004.     if ((PARSE_TYPE & flag) && nam.nam$b_type &&
  2005.         cur_len+nam.nam$b_type < cp_len) {
  2006.     cur_len += nam.nam$b_type;
  2007.     strncat(cp, nam.nam$l_type, (int)nam.nam$b_type);
  2008.     }
  2009.     if ((PARSE_VERSION & flag) && nam.nam$b_ver &&
  2010.         cur_len+nam.nam$b_ver < cp_len) {
  2011.     cur_len += nam.nam$b_ver;
  2012.     strncat(cp, nam.nam$l_ver, (int)nam.nam$b_ver);
  2013.     }
  2014.     return(cur_len);
  2015. }
  2016.  
  2017. /*  Z S A T T R  --  Fill in a Kermit attribute structure for current file.  */
  2018.  
  2019. /*
  2020.  Fills in a Kermit file attribute structure for the file which is to be sent.
  2021.  Returns 0 on success with the structure filled in, or -1 on failure.
  2022.  If any string member is null, then it should be ignored.
  2023.  If any numeric member is -1, then it should be ignored.
  2024. */
  2025. int
  2026. zsattr(xx) struct zattr *xx; {
  2027.     long k;
  2028.     int x;
  2029.     static char mth[13][4] = {    "JAN","FEB","MAR","APR",
  2030.                 "MAY","JUN","JUL","AUG",
  2031.                 "SEP","OCT","NOV","DEC",
  2032.                 ""};
  2033.     static char recfm[15];        /* record format */
  2034.     static char cdate[20];          /* Creation date [yy]yymmdd[hh:mm[:ss]]*/
  2035.     static char creater_id[31];        /* Creator ID string */
  2036.     static unsigned char genprot;   /* Generic protection */
  2037.     static unsigned short lclprot;  /* Local protection */
  2038.     static long sysparam_size=0;    /* Length of system paramater buffer */
  2039.     static char *sysparam_adr=0;    /* Address of system paramater buffer */
  2040.     char type;                /* File type */
  2041.     short int asctim_retlen;
  2042.     char asctim_buf[24];        /* Work buffer for ASCTIM() */
  2043.     struct dsc$descriptor_s
  2044.      asctim_dsc = {sizeof(asctim_buf),DSC$K_DTYPE_T,DSC$K_CLASS_S,
  2045.              (char *)&asctim_buf};
  2046.     static long int i;
  2047.     static unsigned short id_len;
  2048.     static struct dsc$descriptor_s id_str =
  2049.       {31,DSC$K_DTYPE_T,DSC$K_CLASS_S,creater_id};
  2050.  
  2051. /*
  2052.  * Zero out strings
  2053.  */
  2054.  
  2055.     type = 0;
  2056.     recfm[0] = '\0';
  2057.     cdate[0] = '\0';
  2058.     creater_id[0] = '\0';
  2059.     id_len = 0;
  2060.     genprot = 0;            /* Blank protection by default */
  2061.     lclprot = 0;
  2062.  
  2063. /*
  2064.  * See if we are sending "attributes" from a REMOTE command response
  2065.  */
  2066.  
  2067.     if (*nambuf == '\0') {
  2068.     xx->lengthk = 1;        /* Number of 1K blocks rounded up */
  2069.     xx->type.len = 0;        /* File type can't be filled in here */
  2070.     xx->type.val = "";
  2071.     xx->date.len = strlen(cdate);    /* File creation date */
  2072.     xx->date.val = (char *)&cdate;
  2073.     xx->creator.len = strlen(creater_id); /* File creator */
  2074.     xx->creator.val = (char *)&creater_id;
  2075.     xx->account.len = 0;        /* File account */
  2076.     xx->account.val = "";
  2077.     xx->area.len = 0;        /* File area */
  2078.     xx->area.val = "";
  2079.     xx->passwd.len = 0;        /* Area password */
  2080.     xx->passwd.val = "";
  2081.     xx->blksize = -1L;        /* File blocksize */
  2082.     xx->access.len = 0;        /* File access */
  2083.     xx->access.val = "";
  2084.     xx->encoding.len = 1;        /* Transfer syntax */
  2085.     xx->encoding.val = "A";        /* ASCII */
  2086.     xx->disp.len = 0;        /* Disposition upon arrival */
  2087.     xx->disp.val = "";
  2088.     xx->lprotect.len = sizeof(lclprot); /* Local protection */
  2089.     xx->lprotect.val = (char *)&lclprot;
  2090.     xx->gprotect.len = sizeof(genprot); /* Generic protection */
  2091.     xx->gprotect.val = &genprot;
  2092.     xx->systemid.len = 2;        /* System ID for DEC/VMS */
  2093.     xx->systemid.val = "D7";
  2094.     xx->recfm.len = strlen(recfm);    /* Record format */
  2095.     xx->recfm.val = (char *)&recfm;
  2096.     xx->sysparam.len = sysparam_size; /* System-dependent parameters */
  2097.     xx->sysparam.val = sysparam_adr;
  2098.     xx->length = 1;            /* Length */
  2099.     return(0);            /* mumble sweet nothings at it */
  2100.     }
  2101.  
  2102. /*
  2103.  * Load the generic protection
  2104.  */
  2105.  
  2106.     x = xabpro_ifile.xab$w_pro >> XAB$V_WLD;    /* grab returned info */
  2107.     if (!(x & XAB$M_NOREAD))  genprot |= 1+32;    /* Read access */
  2108.     if (!(x & XAB$M_NOWRITE)) genprot |= 2+8;    /* Write+Append access */
  2109.     if (!(x & XAB$M_NOEXE))   genprot |= 4;    /* Execute protection */
  2110.     if (!(x & XAB$M_NODEL))   genprot |= 16;    /* Delete Access */
  2111.     lclprot = xabpro_ifile.xab$w_pro;        /* local protection */
  2112.  
  2113. /*
  2114.  * Convert creation date from an internal value to common ascii string
  2115.  */
  2116.  
  2117.     sys$asctim(&asctim_retlen,&asctim_dsc,&xabdat_ifile.xab$q_cdt,0);
  2118.     asctim_buf[asctim_retlen] = '\0';
  2119.     debug(F110," zsattr asctim_buf",asctim_buf,0);
  2120.     for (x = 0; strncmp(&mth[x],asctim_buf+3,3) ;x++) /* Find month */
  2121.     ;
  2122.     strncpy(cdate,asctim_buf+7,4);    /* 'yyyy' */
  2123.     sprintf(cdate+4,"%02d",x+1);    /* 'mm' */
  2124.     strncpy(cdate+6,asctim_buf+0,2);    /* 'dd' */
  2125.     strncpy(cdate+8,asctim_buf+11,9);    /* ' hh:mm:ss' */
  2126.     if (cdate[6] == ' ')
  2127.     cdate[6] = '0';
  2128.     debug(F110," zsattr cdate",cdate,0);
  2129.  
  2130. /*
  2131.  * Convert the owner UIC into an alpha name
  2132.  */
  2133.  
  2134.     creater_id[0] = '\0';
  2135.     rms_sts = sys$idtoasc(xabpro_ifile.xab$l_uic,&id_len,&id_str,0,0,0);
  2136.     creater_id[id_len] = '\0';            /* terminating null, please */
  2137.     debug(F111," zsattr $idtoasc owner",creater_id,strlen(creater_id));
  2138.     if (rms_sts == SS$_NOSUCHID || rms_sts == SS$_IVIDENT) {
  2139.     creater_id[0] = '\0';
  2140.     rms_sts = SS$_NORMAL;            /* if unknown, null it out */
  2141.     }
  2142.     if (!(rms_sts & 1)) {
  2143.     debug(F101," zsattr $idtoasc failed, status","",rms_sts);
  2144.     return(-1);                /* fatal */
  2145.     }
  2146.  
  2147. /*
  2148.  * Fill in the record format blockette
  2149.  */
  2150.  
  2151.     if (fab_ifile.fab$b_rat & (FAB$M_CR | FAB$M_FTN | FAB$M_PRN)) {
  2152.     strcpy(recfm,"AMJ");
  2153.     } else {
  2154.     strcpy(recfm,"F");
  2155.     sprintf(recfm+1,"%05d",xabfhc_ifile.xab$w_lrl);
  2156.     }
  2157.     debug(F111," zsattr recfm",recfm,strlen(recfm));
  2158.  
  2159. /*
  2160.  * Fill in the returned data structure
  2161.  */
  2162.  
  2163.     xx->lengthk = (i+1)/2;        /* Number of 1K blocks rounded up */
  2164.     xx->type.len = 0;            /* File type can't be filled in here */
  2165.     xx->type.val = "";
  2166.     xx->date.len = strlen(cdate);    /* File creation date */
  2167.     xx->date.val = (char *)&cdate;
  2168.     xx->creator.len = strlen(creater_id); /* File creator */
  2169.     xx->creator.val = (char *)&creater_id;
  2170.     xx->account.len = 0;        /* File account */
  2171.     xx->account.val = "";
  2172.     xx->area.len = 0;            /* File area */
  2173.     xx->area.val = "";
  2174.     xx->passwd.len = 0;            /* Area password */
  2175.     xx->passwd.val = "";
  2176.     xx->blksize = -1L;            /* File blocksize */
  2177.     xx->access.len = 0;            /* File access */
  2178.     xx->access.val = "";
  2179.     xx->encoding.len = 1;        /* Transfer syntax */
  2180.     xx->encoding.val = "A";        /* ASCII */
  2181.     xx->disp.len = 0;            /* Disposition upon arrival */
  2182.     xx->disp.val = "";
  2183.     xx->lprotect.len = sizeof(lclprot); /* Local protection */
  2184.     xx->lprotect.val = (char *)&lclprot;
  2185.     xx->gprotect.len = sizeof(genprot);    /* Generic protection */
  2186.     xx->gprotect.val = &genprot;
  2187.     xx->systemid.len = 2;        /* System ID for DEC/VMS */
  2188.     xx->systemid.val = "D7";
  2189.     xx->recfm.len = strlen(recfm);    /* Record format */
  2190.     xx->recfm.val = (char *)&recfm;
  2191.     xx->sysparam.len = sysparam_size;    /* System-dependent parameters */
  2192.     xx->sysparam.val = sysparam_adr;
  2193.     xx->length = iflen;            /* Length */
  2194.     return(0);
  2195. }
  2196.  
  2197. /*  Z M A I L  --  Send file f as mail to address p.  */
  2198. /*
  2199.   Returns 0 on success
  2200.    2 if mail delivered but temp file can't be deleted
  2201.   -2 if mail can't be delivered
  2202. */
  2203. int
  2204. zmail(p,f) char *p; char *f; {
  2205.     char *zmbuf;
  2206.     static char spbuf[] = "$ mail %s %s/subj=\"Enclosed file %s\"";
  2207.     static char spbuf2[] = "%s;";
  2208.     unsigned long int sts;
  2209.  
  2210.     zmbuf = malloc(strlen(p)+(2*strlen(f))+sizeof(spbuf));
  2211.     sprintf(zmbuf,spbuf, f, p, f);
  2212.     sts = system(zmbuf);
  2213.     debug(F111," zmail: system returns status ",zmbuf,sts);
  2214.     free(zmbuf);
  2215.     if ((sts&1) != 1) {
  2216.       debug(F101," zmail: returning","",-2);
  2217.       return(-2);
  2218.     }
  2219.     zmbuf = malloc(strlen(f)+sizeof(spbuf2));
  2220.     sprintf(zmbuf,spbuf2, f);
  2221.     sts = delete(zmbuf);
  2222.     debug(F111," zmail: delete returns status ",zmbuf,sts);
  2223.     free(zmbuf);
  2224.     if (sts) sts = 2;
  2225.     debug(F101," zmail: returning","",sts);
  2226.     return(sts);
  2227. }
  2228.  
  2229. /* Z P R I N T  --  Print file f with options p.  */
  2230. /*
  2231.   Returns 0 on success, -3 on failure.
  2232. */
  2233. int
  2234. zprint(p,f) char *p; char *f; {
  2235.     char *zmbuf;
  2236.     static char spbuf[] = "$ print/delete %s %s";
  2237.     unsigned long int sts;
  2238.  
  2239.     zmbuf = malloc(strlen(p)+strlen(f)+sizeof(spbuf));
  2240.     sprintf(zmbuf,spbuf, p, f);
  2241.     sts = system(zmbuf);
  2242.     debug(F111," zprint: system returns status ",zmbuf,sts);
  2243.     free(zmbuf);
  2244.     debug(F101," zprint: returning","",(sts&1) ? 0 : -3);
  2245.     return((sts&1) ? 0 : -3);
  2246. }
  2247.  
  2248. /* Z S Y S C M D  --  Execute a DCL command with direct output.  */
  2249.  
  2250. /*
  2251.  * Since it's really difficult to have an alternate CLI under VMS (since the
  2252.  * MCR interface isn't documented and POSIX hasn't published the interface,
  2253.  * we'll just assume everybody uses DCL and hand it of to zshcmd().
  2254.  */
  2255. int
  2256. zsyscmd(s) char *s; {
  2257.     return(zshcmd(s));
  2258. }
  2259.  
  2260. /* Z S H C M D  --  Execute a default CLI command with direct output.  */
  2261.  
  2262. /*
  2263.  * As it's _REALLY_ unlikely that the user is using MCR as his default CLI,
  2264.  * and DEC doesn't document how to write any other alternate CLIs, use DCL.
  2265.  */
  2266.  
  2267. #ifndef    SS$_EXPRCLM        /* VMS doesn't return this yet, but let's */
  2268. #define SS$_EXPRCLM 10804    /* be forward-thinking and anticpate VMS */
  2269. #endif /* SS$_EXPRCLM */    /* V6.0, which will return it. */
  2270. int
  2271. zshcmd(s) char *s; {
  2272.     unsigned long sts;
  2273.     int (*cct)();
  2274.     struct dsc$descriptor_s
  2275.     cmd_line = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
  2276.  
  2277.     if (check_spawn() != 0) {
  2278.     debug(F100," zshcmd: spawning prohibited by UAF flags","",0);
  2279.     return(0);
  2280.     }
  2281.  
  2282.     cct = signal(SIGINT,SIG_DFL);    /* Let inferior process catch ^C */
  2283.  
  2284.     cmd_line.dsc$w_length = strlen(s);
  2285.     cmd_line.dsc$a_pointer = s;
  2286.  
  2287.     if (!(*s))
  2288.     printf("Type LOGOUT to return to VMS C-Kermit.\n\n");
  2289.  
  2290.     sts = lib$spawn(&cmd_line, 0, 0, 0, 0, 0);
  2291.  
  2292.     signal(SIGINT,cct);
  2293.  
  2294. /*
  2295.  * Note: We can't check for this beforehand as doing a getjpi for prclm will
  2296.  *     only return the UAF value, not the available value. So we try it and
  2297.  *     print this message if it didn't work.
  2298.  */
  2299.  
  2300.     if ((sts == SS$_EXQUOTA) || (sts == SS$_EXPRCLM)) {
  2301.     printf("Your account does not have sufficient quotas to use this \
  2302. command.\n");
  2303.     printf("Please ask your system manager to increase your UAF PRCLM \
  2304. quota.\n");
  2305.     }
  2306.  
  2307.     debug(F101,"zshcmd lib$spawn status", "", sts);
  2308.     return(0);
  2309. }
  2310.  
  2311. /*  Z S T R I P  --  Strip device & directory name from file specification.  */
  2312.  
  2313. /*  Strip pathname from filename "name", return pointer to result in name2 */
  2314.  
  2315. static char work[257];    /* buffer for use by zstrip and zltor */
  2316.  
  2317. VOID
  2318. zstrip(name,name2) char *name, **name2; {
  2319.     char *cp, *pp;
  2320.     debug(F110," zstrip before",name,0);
  2321.     pp = work;
  2322.  
  2323. /*  NODE::DEV:[DIR] terminates on on final ':', '>' or ']'.  */
  2324.  
  2325.     for (cp = name; *cp != '\0'; cp++) {
  2326.         if (*cp == '/' || *cp == ':' || *cp == '>' || *cp == ']') /* slash? */
  2327.       pp = work;
  2328.     else if (*cp == ';')        /* Chop off version number */
  2329.       break;
  2330.     else                /* Part of filename */
  2331.       *pp++ = *cp;
  2332.     }
  2333.     *pp = '\0';                /* Terminate the string */
  2334.     *name2 = work;
  2335.     debug(F110," zstrip after",*name2,0);
  2336. }
  2337.  
  2338. int
  2339. zchkpath(s) char *s; {
  2340. /*
  2341.   This needs to be replaced with something more intelligent.
  2342.   The idea is to see if the file, whose specification is pointed to by s,
  2343.   is in the current directory.  This function should return 0 if it s,
  2344.   nonzero otherwise.  Presently we rely on being called with a full
  2345.   filespec of the form DISK:[DEV]NAME.TYP;V, so this works more or less
  2346.   by accident.  What we really need is to call some kind of VMS service
  2347.   to get the NODE::DEV:[DIR] of the file, and compare with the current
  2348.   NODE::DEV:[DIR].
  2349. */
  2350.     char *p;
  2351.     p = zgtdir();            /* Get current dir. */
  2352.     debug(F110,"zchkpath file",s,0);
  2353.     debug(F110,"zchkpath current dir",p,0);
  2354.     return(strncmp(p,s,strlen(p)));    /* Compare it. */
  2355. }
  2356.  
  2357. #ifdef OLD_VMS
  2358. static VOID
  2359. descname(desc,name) struct dsc$descriptor_s *desc; char *name; {
  2360.     desc->dsc$w_length = strlen(name);    /* Length of name */
  2361.     desc->dsc$a_pointer = name;        /* Address */
  2362.     desc->dsc$b_class = DSC$K_CLASS_S;    /* String descriptor class */
  2363.     desc->dsc$b_dtype = DSC$K_DTYPE_T;    /* ASCII string data type */
  2364. }
  2365.  
  2366. /* VMS version of RENAME */
  2367. int /* ? */
  2368. rename(oldname, newname) char oldname[], newname[]; {
  2369.     struct dsc$descriptor_s old_desc, new_desc;
  2370.     int lib$rename_file();
  2371.  
  2372.     /* Build string descriptors */
  2373.  
  2374.     descname(&old_desc, oldname);
  2375.     descname(&new_desc, newname);
  2376.  
  2377.     /* Call lib$rename_file routine */
  2378.  
  2379.     return(lib$rename_file(&old_desc, &new_desc, 0,0,0,0,0,0,0,0,0,0));
  2380. }
  2381. #endif /* OLD_VMS */
  2382.  
  2383. /*
  2384.  * Check to see if we have spawn priv's.
  2385.  */
  2386. int
  2387. check_spawn() {
  2388.     struct itmlstdef {
  2389.     short int buflen;
  2390.     short int itmcod;
  2391.     char *bufaddr;
  2392.     long int *retlen;
  2393.     };
  2394.  
  2395.     struct itmlstdef itmlst[] =
  2396.     {4,JPI$_UAF_FLAGS,0,0,0,0,0,0};
  2397.  
  2398.     unsigned long uaf_flags, uaf_flags_size;
  2399.  
  2400.     itmlst[0].bufaddr = (char *)&uaf_flags;
  2401.     itmlst[0].retlen = &uaf_flags_size;
  2402.  
  2403.     if ((vms_status = sys$getjpiw(0, 0, 0, &itmlst, 0, 0, 0)) != SS$_NORMAL)
  2404.     return(-1);                /* Assume the worst... */
  2405.  
  2406.     if (uaf_flags & UAI$M_CAPTIVE) {
  2407.     printf("\nThis command cannot be executed. Your account is CAPTIVE.\n\n");
  2408.     return(-1);
  2409.     }
  2410. #ifdef    UAI$M_RESTRICTED            /* for pre-V5.2 systems */
  2411.     if (uaf_flags & UAI$M_RESTRICTED) {
  2412.     printf("\nThis command cannot be executed. Your account is CAPTIVE.\n\n");
  2413.     return(-1);
  2414.     }
  2415. #endif    /* uai$v_restricted */
  2416.     return(0);
  2417. }
  2418.  
  2419. /*
  2420.  * Stuff having to do with SET FILE TYPE LABELED
  2421.  */
  2422. char *
  2423. get_vms_vers() {
  2424.     static char sysver[9];
  2425.     int len;
  2426.     struct itmlst {
  2427.           short int buflen;
  2428.           short int code;
  2429.           char *bufadr;
  2430.           int *retlen;
  2431.           } vms_sysver[2];
  2432.  
  2433.     vms_sysver[0].buflen = 8;
  2434.     vms_sysver[0].code = SYI$_VERSION;
  2435.     vms_sysver[0].bufadr = (char *)&sysver;
  2436.     vms_sysver[0].retlen = &len;
  2437.     vms_sysver[1].buflen = 0;
  2438.     vms_sysver[1].code = 0;
  2439.     sys$getsyiw(0,0,0,&vms_sysver,0,0,0);
  2440.     sysver[8]='\0';
  2441.     len = 7;
  2442.     while (sysver[len] == ' ') {
  2443.     sysver[len] = '\0';
  2444.     len--;
  2445.     }
  2446.     return(sysver);
  2447. }
  2448.  
  2449. VOID
  2450. do_label_send(name) char *name; {
  2451.     int pad_size;
  2452.  
  2453.     zinptr += sprintf(zinptr,"KERMIT LABELED FILE:02D704VERS");
  2454.     zinptr += sprintf(zinptr,"%08d%s", strlen(get_vms_vers()), get_vms_vers());
  2455.     zinptr += sprintf(zinptr,"05KVERS00000008%08d", vernum);
  2456.     zinptr += sprintf(zinptr,"07VMSNAME%08d", strlen(name));
  2457.     zinptr += sprintf(zinptr,"%s", name);
  2458.     zinptr += sprintf(zinptr,"07VMSFILE%08d", 70);
  2459.     memmove(zinptr, &xabpro_ifile.xab$w_pro, 2);
  2460.     zinptr += 2;
  2461.     memmove(zinptr, &xabpro_ifile.xab$l_uic, 4);
  2462.     zinptr += 4;
  2463.     memmove(zinptr, &fab_ifile.fab$b_rfm, 1);
  2464.     zinptr += 1;
  2465.     memmove(zinptr, &fab_ifile.fab$b_org, 1);
  2466.     zinptr += 1;
  2467.     memmove(zinptr, &fab_ifile.fab$b_rat, 1);
  2468.     zinptr += 1;
  2469.     memmove(zinptr, &uchar, 4);            /* Dummy for file chars. */
  2470.     zinptr += 4;
  2471.     memmove(zinptr, &fab_ifile.fab$b_fsz, 1);
  2472.     zinptr += 1;
  2473.     memmove(zinptr, &xabfhc_ifile.xab$w_lrl, 2);
  2474.     zinptr += 2;
  2475.     memmove(zinptr, &fab_ifile.fab$w_mrs, 2);
  2476.     zinptr += 2;
  2477.     memmove(zinptr, &xabfhc_ifile.xab$l_ebk, 4);
  2478.     zinptr += 4;
  2479.     memmove(zinptr, &xabfhc_ifile.xab$w_ffb, 2);
  2480.     zinptr += 2;
  2481.     memmove(zinptr, &xabfhc_ifile.xab$l_hbk, 4);
  2482.     zinptr += 4;
  2483.     memmove(zinptr, &fab_ifile.fab$w_deq, 2);
  2484.     zinptr += 2;
  2485.     memmove(zinptr, &fab_ifile.fab$b_bks, 1);
  2486.     zinptr += 1;
  2487.     memmove(zinptr, &fab_ifile.fab$w_gbc, 2);
  2488.     zinptr += 2;
  2489.     memmove(zinptr, &xabfhc_ifile.xab$w_verlimit, 2);
  2490.     zinptr += 2;
  2491.     memmove(zinptr, &fab_ifile.fab$b_rfm+1, 1);    /* This is fab$b_journal */
  2492.     zinptr += 1;
  2493.     memmove(zinptr, &xabdat_ifile.xab$q_cdt, 8);
  2494.     zinptr += 8;
  2495.     memmove(zinptr, &xabdat_ifile.xab$q_rdt, 8);
  2496.     zinptr += 8;
  2497.     memmove(zinptr, &xabdat_ifile.xab$w_rvn, 2);
  2498.     zinptr += 2;
  2499.     memmove(zinptr, &xabdat_ifile.xab$q_edt, 8);
  2500.     zinptr += 8;
  2501.     memmove(zinptr, &xabdat_ifile.xab$q_bdt, 8);
  2502.     zinptr += 8;
  2503.     if (xabpro_ifile.xab$w_acllen != 0) {
  2504.     zinptr += sprintf(zinptr,"06VMSACL%08d", xabpro_ifile.xab$w_acllen);
  2505.     memmove(zinptr, &aclbuf, xabpro_ifile.xab$w_acllen);
  2506.     zinptr += xabpro_ifile.xab$w_acllen;
  2507.     }
  2508.     zinptr += sprintf(zinptr,"04DATA00000000");
  2509.     zincnt = (zinptr - zinbuffer);        /* Size of this beast */
  2510.     zinptr = zinbuffer;                /* Reset pointer for readout */
  2511. }
  2512.  
  2513. int
  2514. do_label_recv() {
  2515.     char *recv_ptr;
  2516.     char buffer[16];
  2517.     char vmsfile[70];
  2518.     char *filptr = vmsfile;
  2519.     int lblen;
  2520.     int gotname = 0, gotfile = 0, gotacl = 0;
  2521.     int i, j;
  2522.     unsigned short jnlflg;
  2523.  
  2524.     debug(F101," in do_label_recv, options","",ofile_lblopts);
  2525.     ofile_lblproc = 1;                /* Don't come here again */
  2526.  
  2527.     if (strncmp(zoutbuffer,"KERMIT LABELED FILE:02D704VERS",30) != 0)
  2528.     return(0);            /* Just continue if unlabeled */
  2529.  
  2530.     recv_ptr = zoutbuffer+30;        /* start at front of buffer */
  2531.  
  2532.     memcpy(buffer, recv_ptr, 8);
  2533.     recv_ptr += 8;
  2534.     buffer[8] = '\0';
  2535.     lblen = atoi(buffer);
  2536.  
  2537.     memcpy(buffer, recv_ptr, lblen);
  2538.     recv_ptr += lblen;
  2539.     buffer[lblen] = '\0';
  2540.     debug(F110,"  file created under VAX/VMS: ",buffer,0);
  2541.  
  2542.     memcpy(buffer, recv_ptr, 7);
  2543.     recv_ptr += 7;
  2544.     if (strncmp(buffer, "05KVERS", 7) != 0) {
  2545.     debug(F100,"  lost sync at KVERS","",0);
  2546.     return(-1);
  2547.     }
  2548.  
  2549.     memcpy(buffer, recv_ptr, 8);
  2550.     recv_ptr += 8;
  2551.     buffer[8] = '\0';
  2552.     lblen = atoi(buffer);
  2553.  
  2554.     memcpy(buffer, recv_ptr, lblen);
  2555.     recv_ptr += lblen;
  2556.     buffer[lblen] = '\0';
  2557.     debug(F110,"  file created with C-Kermit/VMS: ",buffer,0);
  2558.  
  2559.     next_label:
  2560.     memcpy(buffer, recv_ptr, 2);
  2561.     recv_ptr += 2;
  2562.     buffer[2] = '\0';
  2563.     lblen = atoi(buffer);
  2564.     if (lblen == 0) {
  2565.     debug(F100,"  lost sync at next_label: ","",0);
  2566.     return(-1);
  2567.     }
  2568.  
  2569.     memcpy(buffer, recv_ptr, lblen);
  2570.     recv_ptr += lblen;
  2571.     buffer[lblen] = '\0';
  2572.     debug(F110,"  found tag: ",buffer,0);
  2573.     if (strcmp(buffer, "VMSNAME") == 0) {
  2574.     memcpy(buffer, recv_ptr, 8);
  2575.         recv_ptr += 8;
  2576.     buffer[8] = '\0';
  2577.     lblen = atoi(buffer);
  2578.     memcpy(ofile_vmsname, recv_ptr, lblen);
  2579.         recv_ptr += lblen;
  2580.     ofile_vmsname[lblen] = '\0';
  2581.     gotname++;
  2582.     debug(F110,"  loaded file name block as: ",ofile_vmsname,0);
  2583.     i = strstr(ofile_vmsname, "::");
  2584.     if (i != NULL) {
  2585.         i += 2;
  2586.         memmove(ofile_vmsname, i, strlen(ofile_vmsname));
  2587.     }
  2588.         if ((ofile_lblopts & LBL_PTH) == 0) {
  2589.         i = strrchr(ofile_vmsname, ':');
  2590.         j = strrchr(ofile_vmsname, ']');
  2591.         if (j == NULL)
  2592.         j = strrchr(ofile_vmsname, '>');
  2593.         if (j > i)
  2594.         i = j;
  2595.         i++;
  2596.         memmove(ofile_vmsname, i, strlen(ofile_vmsname));
  2597.     }
  2598.     if (strchr(ofile_vmsname, ';') != NULL) {
  2599.         for (j = strlen(ofile_vmsname); ofile_vmsname[j] != ';'; j--)
  2600.         ;
  2601.         ofile_vmsname[j] = '\0';
  2602.     }
  2603.     debug(F110,"  resultant filespec: ",ofile_vmsname,0);
  2604.     goto next_label;
  2605.     }
  2606.     else if (strcmp(buffer, "VMSFILE") == 0) {
  2607.     memcpy(buffer, recv_ptr, 8);
  2608.     recv_ptr += 8;
  2609.     buffer[8] = '\0';
  2610.     lblen = atoi(buffer);
  2611.     memcpy(vmsfile, recv_ptr, lblen);
  2612.     recv_ptr += lblen;
  2613.     vmsfile[lblen] = '\0';
  2614.     gotfile++;
  2615.     debug(F100,"  loaded file attribute block","",0);
  2616.     goto next_label;
  2617.     }
  2618.     else if (strcmp(buffer, "VMSACL") == 0) {
  2619.     memcpy(buffer, recv_ptr, 8);
  2620.     recv_ptr += 8;
  2621.     buffer[8] = '\0';
  2622.     ofile_acllen = atoi(buffer);
  2623.     memcpy(ofile_vmsacl, recv_ptr, ofile_acllen);
  2624.     recv_ptr += ofile_acllen;
  2625.     ofile_vmsacl[ofile_acllen] = '\0';
  2626.     gotacl++;
  2627.     debug(F100,"  loaded file ACL block","",0);
  2628.     goto next_label;
  2629.     }
  2630.     else if (strcmp(buffer, "DATA") == 0) {
  2631.     memcpy(buffer, recv_ptr, 8);
  2632.     recv_ptr += 8;
  2633.     buffer[8] = '\0';
  2634.     lblen = atoi(buffer);
  2635.     if (lblen != 0) {
  2636.         debug(F101,"  length of DATA tag not zero","",lblen);
  2637.         return(-1);
  2638.     }
  2639.     debug(F100,"  positioned at start of file data","",0);
  2640.     goto all_set;
  2641.     }
  2642.     else {
  2643.     debug(F110,"  unrecognized label: ",buffer,0);
  2644.     memcpy(buffer, recv_ptr, 8);
  2645.     recv_ptr += 8;
  2646.     buffer[8] = '\0';
  2647.     lblen = atoi(buffer);
  2648.     recv_ptr += lblen;
  2649.     goto next_label;
  2650.     }
  2651.  
  2652.     all_set:
  2653.     if (gotfile != 1 || gotname != 1) {
  2654.     debug(F100,"  missing one or more required labels","",0);
  2655.     return(-1);
  2656.     }
  2657.  
  2658. /*
  2659.  * Prep the characteristics
  2660.  */
  2661.  
  2662.     fab_ofile.fab$b_fac = FAB$M_BIO | FAB$M_PUT;
  2663.     fab_ofile.fab$l_fop = FAB$M_MXV;
  2664.     if ((ofile_lblopts & LBL_NAM) != 0) {
  2665.     fab_ofile.fab$l_fna = ofile_vmsname;
  2666.     fab_ofile.fab$b_fns = strlen(ofile_vmsname);
  2667.     }
  2668.     fab_ofile.fab$l_xab = (char *)&xabdat_ofile;
  2669.     rab_ofile = cc$rms_rab;
  2670.     rab_ofile.rab$l_fab = &fab_ofile;
  2671.     xabdat_ofile = cc$rms_xabdat;
  2672.     xabdat_ofile.xab$l_nxt = (char *)&xabrdt_ofile;
  2673.     xabrdt_ofile = cc$rms_xabrdt;
  2674.     xabrdt_ofile.xab$l_nxt = (char *)&xabfhc_ofile;
  2675.     xabfhc_ofile = cc$rms_xabfhc;
  2676.     xabfhc_ofile.xab$l_nxt = (char *)&xabpro_ofile;
  2677.     xabpro_ofile = cc$rms_xabpro;
  2678.     xabpro_ofile.xab$l_nxt = (char *)&xaball_ofile;
  2679.     xaball_ofile = cc$rms_xaball;
  2680.  
  2681. /*
  2682.  * Load 'em up
  2683.  */
  2684.  
  2685.     memmove(&xabpro_ofile.xab$w_pro, filptr, 2);
  2686.     filptr += 2;
  2687.     if ((ofile_lblopts & LBL_OWN) != 0)
  2688.     memmove(&xabpro_ofile.xab$l_uic, filptr, 4);
  2689.     filptr += 4;
  2690.     memmove(&fab_ofile.fab$b_rfm, filptr, 1);
  2691.     filptr += 1;
  2692.     memmove(&fab_ofile.fab$b_org, filptr, 1);
  2693.     filptr += 1;
  2694.     memmove(&fab_ofile.fab$b_rat, filptr, 1);
  2695.     filptr += 1;
  2696.     filptr += 4;            /* reserved */
  2697.     memmove(&fab_ofile.fab$b_fsz, filptr, 1);
  2698.     filptr += 1;
  2699.     memmove(&xabfhc_ofile.xab$w_lrl, filptr, 2);
  2700.     filptr += 2;
  2701.     memmove(&fab_ofile.fab$w_mrs, filptr, 2);
  2702.     filptr += 2;
  2703.     memmove(&xabfhc_ofile.xab$l_ebk, filptr, 4);
  2704.     filptr += 4;
  2705. /* preserve this as RMS won't remember it for us */
  2706.     memmove(&ofile_ffb, filptr, 2);
  2707.     filptr += 2;
  2708.     memmove(&xaball_ofile.xab$l_alq, filptr, 4);
  2709.     filptr += 4;
  2710.     memmove(&xaball_ofile.xab$w_deq, filptr, 2);
  2711.     filptr += 2;
  2712.     memmove(&xaball_ofile.xab$b_bkz, filptr, 1);
  2713.     filptr += 1;
  2714.     memmove(&fab_ofile.fab$w_gbc, filptr, 2);
  2715.     filptr += 2;
  2716.     memmove(&xabfhc_ofile.xab$w_verlimit, filptr, 2);
  2717.     filptr += 2;
  2718.     memmove(&jnlflg, filptr, 1);
  2719.     if (jnlflg !=0)
  2720.     debug(F100,"  journaling status removed for file","",0);
  2721.     filptr += 1;
  2722.     memmove(&xabdat_ofile.xab$q_cdt, filptr, 8);
  2723.     filptr += 8;
  2724.     memmove(&revdat, filptr, 8);
  2725.     filptr += 8;
  2726.     memmove(&revnum, filptr, 2);
  2727.     filptr += 2;
  2728.     memmove(&xabdat_ofile.xab$q_edt, filptr, 8);
  2729.     filptr += 8;
  2730.     if ((ofile_lblopts & LBL_BCK) != 0)
  2731.     memmove(&xabdat_ofile.xab$q_bdt, filptr, 8);
  2732.     filptr += 8;
  2733.  
  2734. /*
  2735.  * ACL's?
  2736.  */
  2737.  
  2738.     if ((ofile_lblopts & LBL_ACL) != 0 && gotacl != 0) {
  2739.     xabpro_ofile.xab$l_aclbuf = (char *)&ofile_vmsacl;
  2740.     xabpro_ofile.xab$w_aclsiz = ofile_acllen;
  2741.     }
  2742.  
  2743. /*
  2744.  * Give it a quick whirl around the dance floor
  2745.  */
  2746.  
  2747.     rms_sts = sys$create(&fab_ofile);
  2748.     if (!(rms_sts & 1)) {
  2749.     debug(F101,"  $create failed, status","",rms_sts);
  2750.     return(-1);
  2751.     }
  2752.  
  2753.     if((ofile_lblopts & LBL_ACL) != 0 && gotacl != 0) {
  2754.     if (!(xabpro_ofile.xab$l_aclsts & 1)) {
  2755.         debug(F101,"  ACL chain failed, status","",xabpro_ofile.xab$l_aclsts);
  2756.         return(-1);
  2757.     }
  2758.     }
  2759.  
  2760.     rms_sts = sys$connect(&rab_ofile);
  2761.     if (!(rms_sts & 1)) {
  2762.     debug(F101,"  $connect failed, status","",rms_sts);
  2763.     return(-1);
  2764.     }
  2765.  
  2766. /*
  2767.  * Slide the remainder of the data to the head of the buffer and adjust the
  2768.  * counter and pointer. This will cause the buffer to be re-filled to the full
  2769.  * 32Kb capacity, which is necessary for proper operation of zoutdump().
  2770.  */
  2771.  
  2772.     zoutcnt -= ((char *)recv_ptr - (char *)zoutbuffer);
  2773.     memcpy(zoutbuffer, recv_ptr, zoutcnt);
  2774.     zoutptr = zoutbuffer + zoutcnt;
  2775.     return(1);                /* Go fill some more */
  2776. }
  2777.